Skip to content

Commit

Permalink
Move cloud-init files to ~/.ubuntupro/.cloud-init
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardGomezEscandell committed Feb 19, 2024
1 parent 3633ffc commit 2429ea7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
18 changes: 8 additions & 10 deletions windows-agent/internal/cloudinit/cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@ type Config interface {

// CloudInit contains necessary data to drop cloud-init user data files for WSL's data source to pick them up.
type CloudInit struct {
globalDataDir string
distroDataDir string
conf Config
dataDir string
conf Config
}

// New creates a CloudInit object and attaches it to the configuration notifier.
func New(ctx context.Context, conf Config, dataDir string) (CloudInit, error) {
func New(ctx context.Context, conf Config, publicDir string) (CloudInit, error) {
c := CloudInit{
globalDataDir: dataDir,
distroDataDir: filepath.Join(dataDir, "landscape"),
conf: conf,
dataDir: filepath.Join(publicDir, ".cloud-init"),
conf: conf,
}

if err := c.WriteAgentData(ctx); err != nil {
Expand All @@ -62,7 +60,7 @@ func (c CloudInit) WriteAgentData(ctx context.Context) (err error) {
return err
}

err = writeFileInDir(c.globalDataDir, "agent.yaml", cloudInit)
err = writeFileInDir(c.dataDir, "agent.yaml", cloudInit)
if err != nil {
return err
}
Expand All @@ -72,7 +70,7 @@ func (c CloudInit) WriteAgentData(ctx context.Context) (err error) {

// WriteDistroData writes cloud-init user data to be used for a distro in particular.
func (c CloudInit) WriteDistroData(distroName string, cloudInit string) error {
err := writeFileInDir(c.distroDataDir, distroName+".user-data", []byte(cloudInit))
err := writeFileInDir(c.dataDir, distroName+".user-data", []byte(cloudInit))
if err != nil {
return fmt.Errorf("could not create distro-specific cloud-init file: %v", err)
}
Expand Down Expand Up @@ -109,7 +107,7 @@ func writeFileInDir(dir string, file string, contents []byte) error {
func (c CloudInit) RemoveDistroData(distroName string) (err error) {
defer decorate.OnError(&err, "could not remove distro-specific cloud-init file")

path := filepath.Join(c.distroDataDir, distroName+".user-data")
path := filepath.Join(c.dataDir, distroName+".user-data")

err = os.Remove(path)
if errors.Is(err, fs.ErrNotExist) {
Expand Down
30 changes: 15 additions & 15 deletions windows-agent/internal/cloudinit/cloudinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func TestNew(t *testing.T) {

ctx := context.Background()

dataDir := t.TempDir()
publicDir := t.TempDir()

conf := &mockConfig{
subcriptionErr: tc.breakWriteAgentData,
}

_, err := cloudinit.New(ctx, conf, dataDir)
_, err := cloudinit.New(ctx, conf, publicDir)
if tc.wantErr {
require.Error(t, err, "Cloud-init creation should have returned an error")
return
Expand All @@ -48,7 +48,7 @@ func TestNew(t *testing.T) {
require.Len(t, conf.notify, 1, "Cloud-init should have attached a callback to the config")

// Assert that the subscribed function works
path := filepath.Join(dataDir, "agent.yaml")
path := filepath.Join(publicDir, ".cloud-init", "agent.yaml")
require.NoErrorf(t, os.RemoveAll(path), "Removing the agent cloud-init should not fail")

conf.triggerNotify()
Expand Down Expand Up @@ -122,8 +122,8 @@ url = www.example.com/new/rickroll
t.Parallel()
ctx := context.Background()

root := t.TempDir()
dir := filepath.Join(root, "cloud-init")
publicDir := t.TempDir()
dir := filepath.Join(publicDir, ".cloud-init")
path := filepath.Join(dir, "agent.yaml")

conf := &mockConfig{
Expand All @@ -132,7 +132,7 @@ url = www.example.com/new/rickroll
}

// Test a clean filesystem (New calls WriteAgentData internally)
ci, err := cloudinit.New(ctx, conf, dir)
ci, err := cloudinit.New(ctx, conf, publicDir)
require.NoError(t, err, "cloudinit.New should return no error")
require.FileExists(t, path, "New() should have created an agent cloud-init file")

Expand Down Expand Up @@ -263,14 +263,14 @@ data:

distroName := "CoolDistro"

root := t.TempDir()
dir := filepath.Join(root, "cloud-init")
path := filepath.Join(dir, "landscape", distroName+".user-data")
publicDir := t.TempDir()
dir := filepath.Join(publicDir, ".cloud-init")
path := filepath.Join(dir, distroName+".user-data")

conf := &mockConfig{}

// Test a clean filesystem (New calls WriteAgentData internally)
ci, err := cloudinit.New(ctx, conf, dir)
ci, err := cloudinit.New(ctx, conf, publicDir)
require.NoError(t, err, "Setup: cloud-init New should return no errors")

if !tc.noOldData {
Expand Down Expand Up @@ -343,15 +343,15 @@ func TestRemoveDistroData(t *testing.T) {

distroName := "CoolDistro"

root := t.TempDir()
distroDir := filepath.Join(root, "landscape")
path := filepath.Join(distroDir, distroName+".user-data")
publicDir := t.TempDir()
dir := filepath.Join(publicDir, ".cloud-init")
path := filepath.Join(dir, distroName+".user-data")

ci, err := cloudinit.New(ctx, &mockConfig{}, root)
ci, err := cloudinit.New(ctx, &mockConfig{}, publicDir)
require.NoError(t, err, "Setup: cloud-init New should return no errors")

if !tc.dirDoesNotExist {
require.NoError(t, os.MkdirAll(distroDir, 0700), "Setup: could not set up directory")
require.NoError(t, os.MkdirAll(dir, 0700), "Setup: could not set up directory")
if !tc.fileDoesNotExist {
require.NoError(t, os.WriteFile(path, []byte("hello, world!"), 0600), "Setup: could not set up directory")
}
Expand Down

0 comments on commit 2429ea7

Please sign in to comment.