Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
abbac367
Commit
abbac367
authored
Sep 20, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
provisioner/shell: convert windows line endings to Unix [GH-277]
parent
dfb4e80d
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
138 additions
and
0 deletions
+138
-0
CHANGELOG.md
CHANGELOG.md
+3
-0
provisioner/shell/provisioner.go
provisioner/shell/provisioner.go
+10
-0
provisioner/shell/unix_reader.go
provisioner/shell/unix_reader.go
+88
-0
provisioner/shell/unix_reader_test.go
provisioner/shell/unix_reader_test.go
+33
-0
website/source/docs/provisioners/shell.html.markdown
website/source/docs/provisioners/shell.html.markdown
+4
-0
No files found.
CHANGELOG.md
View file @
abbac367
...
...
@@ -13,6 +13,9 @@ IMPROVEMENTS:
*
core: User variables can now be used for integer, boolean, etc.
values. [GH-418]
*
builder/amazon/all: Interrupts work while waiting for AMI to be ready.
*
provisioner/shell: Script line-endings are automatically converted to
Unix-style line-endings. Can be disabled by setting "binary" to "true".
[GH-277]
BUG FIXES:
...
...
provisioner/shell/provisioner.go
View file @
abbac367
...
...
@@ -8,6 +8,7 @@ import (
"fmt"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
"io"
"io/ioutil"
"log"
"os"
...
...
@@ -20,6 +21,10 @@ const DefaultRemotePath = "/tmp/script.sh"
type
config
struct
{
common
.
PackerConfig
`mapstructure:",squash"`
// If true, the script contains binary and line endings will not be
// converted from Windows to Unix-style.
Binary
bool
// An inline script to execute. Multiple strings are all executed
// in the context of a single shell.
Inline
[]
string
...
...
@@ -259,6 +264,11 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
return
err
}
var
r
io
.
Reader
=
f
if
!
p
.
config
.
Binary
{
r
=
&
UnixReader
{
Reader
:
r
}
}
if
err
:=
comm
.
Upload
(
p
.
config
.
RemotePath
,
f
);
err
!=
nil
{
return
fmt
.
Errorf
(
"Error uploading script: %s"
,
err
)
}
...
...
provisioner/shell/unix_reader.go
0 → 100644
View file @
abbac367
package
shell
import
(
"bufio"
"bytes"
"io"
"sync"
)
// UnixReader is a Reader implementation that automatically converts
// Windows line endings to Unix line endings.
type
UnixReader
struct
{
Reader
io
.
Reader
buf
[]
byte
once
sync
.
Once
scanner
*
bufio
.
Scanner
}
func
(
r
*
UnixReader
)
Read
(
p
[]
byte
)
(
n
int
,
err
error
)
{
// Create the buffered reader once
r
.
once
.
Do
(
func
()
{
r
.
scanner
=
bufio
.
NewScanner
(
r
.
Reader
)
r
.
scanner
.
Split
(
scanUnixLine
)
})
// If we have no data in our buffer, scan to the next token
if
len
(
r
.
buf
)
==
0
{
if
!
r
.
scanner
.
Scan
()
{
err
=
r
.
scanner
.
Err
()
if
err
==
nil
{
err
=
io
.
EOF
}
return
0
,
err
}
r
.
buf
=
r
.
scanner
.
Bytes
()
}
// Write out as much data as we can to the buffer, storing the rest
// for the next read.
n
=
len
(
p
)
if
n
>
len
(
r
.
buf
)
{
n
=
len
(
r
.
buf
)
}
copy
(
p
,
r
.
buf
)
r
.
buf
=
r
.
buf
[
n
:
]
return
}
// scanUnixLine is a bufio.Scanner SplitFunc. It tokenizes on lines, but
// only returns unix-style lines. So even if the line is "one\r\n", the
// token returned will be "one\n".
func
scanUnixLine
(
data
[]
byte
,
atEOF
bool
)
(
advance
int
,
token
[]
byte
,
err
error
)
{
if
atEOF
&&
len
(
data
)
==
0
{
return
0
,
nil
,
nil
}
if
i
:=
bytes
.
IndexByte
(
data
,
'\n'
);
i
>=
0
{
// We have a new-line terminated line. Return the line with the newline
return
i
+
1
,
dropCR
(
data
[
0
:
i
+
1
]),
nil
}
if
atEOF
{
// We have a final, non-terminated line
return
len
(
data
),
dropCR
(
data
),
nil
}
if
data
[
len
(
data
)
-
1
]
!=
'\r'
{
// We have a normal line, just let it tokenize
return
len
(
data
),
data
,
nil
}
// We need more data
return
0
,
nil
,
nil
}
func
dropCR
(
data
[]
byte
)
[]
byte
{
if
len
(
data
)
>
0
&&
data
[
len
(
data
)
-
2
]
==
'\r'
{
// Trim off the last byte and replace it with a '\n'
data
=
data
[
0
:
len
(
data
)
-
1
]
data
[
len
(
data
)
-
1
]
=
'\n'
}
return
data
}
provisioner/shell/unix_reader_test.go
0 → 100644
View file @
abbac367
package
shell
import
(
"bytes"
"io"
"testing"
)
func
TestUnixReader_impl
(
t
*
testing
.
T
)
{
var
raw
interface
{}
raw
=
new
(
UnixReader
)
if
_
,
ok
:=
raw
.
(
io
.
Reader
);
!
ok
{
t
.
Fatal
(
"should be reader"
)
}
}
func
TestUnixReader
(
t
*
testing
.
T
)
{
input
:=
"one
\r\n
two
\n
three
\r\n
"
expected
:=
"one
\n
two
\n
three
\n
"
r
:=
&
UnixReader
{
Reader
:
bytes
.
NewReader
([]
byte
(
input
)),
}
result
:=
new
(
bytes
.
Buffer
)
if
_
,
err
:=
io
.
Copy
(
result
,
r
);
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
result
.
String
()
!=
expected
{
t
.
Fatalf
(
"bad: %#v"
,
result
.
String
())
}
}
website/source/docs/provisioners/shell.html.markdown
View file @
abbac367
...
...
@@ -47,6 +47,10 @@ Exactly _one_ of the following is required:
Optional parameters:
*
`binary`
(boolean) - If true, specifies that the script(s) are binary
files, and Packer should therefore not convert Windows line endings to
Unix line endings (if there are any). By default this is false.
*
`environment_vars`
(array of strings) - An array of key/value pairs
to inject prior to the execute_command. The format should be
`key=value`
. Packer injects some environmental variables by default
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment