-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(instance): import file for cloud-init
- Loading branch information
Showing
7 changed files
with
120 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,9 @@ EXAMPLES: | |
Create an instance with 2 local volumes (10GB and 10GB) | ||
scw instance server create image=ubuntu_focal root-volume=local:10GB additional-volumes.0=local:10GB | ||
|
||
Create an instance using cloud-init config file | ||
scw instance server create image=ubuntu_focal [email protected] | ||
|
||
Use an existing IP | ||
ip=$(scw instance ip create | grep id | awk '{ print $2 }') | ||
scw instance server create image=ubuntu_focal ip=$ip | ||
|
@@ -35,7 +38,7 @@ ARGS: | |
[security-group-id] The security group ID it use for this server | ||
[placement-group-id] The placement group ID in witch the server has to be created | ||
[bootscript-id] The bootscript ID to use, if empty the local boot will be used | ||
[cloud-init] The cloud-init script to use | ||
[cloud-init] The cloud-init script to use. Use @filename to import a file | ||
[boot-type=local] The boot type to use, if empty the local boot will be used. Will be overwritten to bootscript if bootscript-id is set. (local | bootscript | rescue) | ||
[project-id] Project ID to use. If none is passed the default project ID will be used | ||
[zone=fr-par-1] Zone to target. If none is passed will use default zone from the config | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package args | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"strings" | ||
) | ||
|
||
// ValueOrFileContent returns file content or value | ||
func ValueOrFileContent(value string) (string, error) { | ||
if !strings.HasPrefix(value, "@") { | ||
return value, nil | ||
} | ||
|
||
content, err := ioutil.ReadFile(value[1:]) | ||
if err != nil { | ||
return "", fmt.Errorf("could not open requested file: %s", err) | ||
} | ||
|
||
return string(content), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package args | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
testFileContent = "test-content" | ||
testFileName = "local-test-file.txt" | ||
) | ||
|
||
func TestValueOrFileContent(t *testing.T) { | ||
type TestCase struct { | ||
arg string | ||
error bool | ||
expected string | ||
} | ||
|
||
run := func(tc TestCase) func(t *testing.T) { | ||
return func(t *testing.T) { | ||
reader, err := ValueOrFileContent(tc.arg) | ||
assert.Equal(t, tc.error, err != nil) | ||
assert.Equal(t, tc.expected, reader) | ||
} | ||
} | ||
|
||
t.Run("empty", run(TestCase{ | ||
arg: "", | ||
expected: "", | ||
})) | ||
|
||
t.Run("plain-value", run(TestCase{ | ||
arg: "value", | ||
expected: "value", | ||
})) | ||
|
||
t.Run("@missing-file", run(TestCase{ | ||
arg: "@missing-file", | ||
expected: "", | ||
error: true, | ||
})) | ||
|
||
// Write a test file to read content | ||
f, err := os.Create(testFileName) | ||
assert.NoError(t, err) | ||
_, err = f.WriteString(testFileContent) | ||
assert.NoError(t, err) | ||
err = f.Close() | ||
assert.NoError(t, err) | ||
|
||
t.Run("@"+testFileName, run(TestCase{ | ||
arg: "@" + testFileName, | ||
expected: testFileContent, | ||
})) | ||
|
||
// Remove tested file | ||
err = os.Remove(testFileName) | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters