Skip to content

Commit

Permalink
Remove task provisioning
Browse files Browse the repository at this point in the history
Cloud-init is in charge of this now
  • Loading branch information
EduardGomezEscandell committed Feb 5, 2024
1 parent eaf82aa commit 1bf2eb8
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 238 deletions.
23 changes: 0 additions & 23 deletions windows-agent/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,6 @@ func (c *Config) Subscription(ctx context.Context) (token string, source Source,
return token, source, nil
}

// ProvisioningTasks returns a slice of all tasks to be submitted upon first contact with a distro.
func (c *Config) ProvisioningTasks(ctx context.Context, distroName string) ([]task.Task, error) {
var taskList []task.Task

// Refresh data from registry
c.mu.Lock()
defer c.mu.Unlock()

if err := c.load(); err != nil {
return nil, fmt.Errorf("could not load: %v", err)
}

// Ubuntu Pro attachment
proToken, _ := c.subscription.resolve()
taskList = append(taskList, tasks.ProAttachment{Token: proToken})

// Landscape config
lconf, _ := c.landscape.resolve()
taskList = append(taskList, tasks.LandscapeConfigure{Config: lconf, HostagentUID: c.landscape.UID})

return taskList, nil
}

// LandscapeClientConfig returns the value of the landscape server URL and
// the method it was acquired with (if any).
func (c *Config) LandscapeClientConfig(ctx context.Context) (string, Source, error) {
Expand Down
75 changes: 8 additions & 67 deletions windows-agent/internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
config "github.com/canonical/ubuntu-pro-for-wsl/windows-agent/internal/config"
"github.com/canonical/ubuntu-pro-for-wsl/windows-agent/internal/contracts"
"github.com/canonical/ubuntu-pro-for-wsl/windows-agent/internal/distros/database"
"github.com/canonical/ubuntu-pro-for-wsl/windows-agent/internal/distros/task"
"github.com/canonical/ubuntu-pro-for-wsl/windows-agent/internal/tasks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
wsl "github.com/ubuntu/gowsl"
Expand Down Expand Up @@ -85,7 +83,7 @@ func TestSubscription(t *testing.T) {
ctx = wsl.WithMock(ctx, wslmock.New())
}

db, err := database.New(ctx, t.TempDir(), nil)
db, err := database.New(ctx, t.TempDir())
require.NoError(t, err, "Setup: could not create empty database")

setup, dir := setUpMockSettings(t, ctx, db, tc.settingsState, tc.breakFile)
Expand Down Expand Up @@ -141,7 +139,7 @@ func TestLandscapeConfig(t *testing.T) {
ctx = wsl.WithMock(ctx, wslmock.New())
}

db, err := database.New(ctx, t.TempDir(), nil)
db, err := database.New(ctx, t.TempDir())
require.NoError(t, err, "Setup: could not create empty database")

setup, dir := setUpMockSettings(t, ctx, db, tc.settingsState, tc.breakFile)
Expand Down Expand Up @@ -191,7 +189,7 @@ func TestLandscapeAgentUID(t *testing.T) {
ctx = wsl.WithMock(ctx, wslmock.New())
}

db, err := database.New(ctx, t.TempDir(), nil)
db, err := database.New(ctx, t.TempDir())
require.NoError(t, err, "Setup: could not create empty database")

setup, dir := setUpMockSettings(t, ctx, db, tc.settingsState, tc.breakFile)
Expand Down Expand Up @@ -222,63 +220,6 @@ func TestLandscapeAgentUID(t *testing.T) {
}
}

func TestProvisioningTasks(t *testing.T) {
if wsl.MockAvailable() {
t.Parallel()
}

testCases := map[string]struct {
settingsState settingsState

wantToken string
wantLandscapeConf string
wantLandscapeUID string

wantError bool
}{
"Success when there is no data": {settingsState: untouched},
"Success when there is an empty config file": {settingsState: fileExists},
"Success when the file's pro token field exists but is empty": {settingsState: userTokenExists},
"Success with a user token": {settingsState: userTokenHasValue, wantToken: "user_token"},
"Success when there is Landscape config": {settingsState: userLandscapeConfigHasValue | landscapeUIDHasValue, wantLandscapeConf: "[client]\nuser=JohnDoe", wantLandscapeUID: "landscapeUID1234"},
}

for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
ctx := context.Background()
if wsl.MockAvailable() {
t.Parallel()
ctx = wsl.WithMock(ctx, wslmock.New())
}

db, err := database.New(ctx, t.TempDir(), nil)
require.NoError(t, err, "Setup: could not create empty database")

setup, dir := setUpMockSettings(t, ctx, db, tc.settingsState, false)
conf := config.New(ctx, dir)
setup(t, conf)

gotTasks, err := conf.ProvisioningTasks(ctx, "UBUNTU")
if tc.wantError {
require.Error(t, err, "ProvisioningTasks should return an error")
return
}
require.NoError(t, err, "ProvisioningTasks should return no error")

wantTasks := []task.Task{
tasks.ProAttachment{Token: tc.wantToken},
tasks.LandscapeConfigure{
Config: tc.wantLandscapeConf,
HostagentUID: tc.wantLandscapeUID,
},
}

require.ElementsMatch(t, wantTasks, gotTasks, "Unexpected contents returned by ProvisioningTasks")
})
}
}

func TestSetUserSubscription(t *testing.T) {
if wsl.MockAvailable() {
t.Parallel()
Expand Down Expand Up @@ -308,7 +249,7 @@ func TestSetUserSubscription(t *testing.T) {
ctx = wsl.WithMock(ctx, wslmock.New())
}

db, err := database.New(ctx, t.TempDir(), nil)
db, err := database.New(ctx, t.TempDir())
require.NoError(t, err, "Setup: could not create empty database")

setup, dir := setUpMockSettings(t, ctx, db, tc.settingsState, tc.breakFile)
Expand Down Expand Up @@ -365,7 +306,7 @@ func TestSetLandscapeAgentUID(t *testing.T) {
ctx = wsl.WithMock(ctx, wslmock.New())
}

db, err := database.New(ctx, t.TempDir(), nil)
db, err := database.New(ctx, t.TempDir())
require.NoError(t, err, "Setup: could not create empty database")

setup, dir := setUpMockSettings(t, ctx, db, tc.settingsState, tc.breakFile)
Expand Down Expand Up @@ -438,7 +379,7 @@ func TestFetchMicrosoftStoreSubscription(t *testing.T) {
ctx = wsl.WithMock(ctx, wslmock.New())
}

db, err := database.New(ctx, t.TempDir(), nil)
db, err := database.New(ctx, t.TempDir())
require.NoError(t, err, "Setup: could not create empty database")

setup, dir := setUpMockSettings(t, ctx, db, tc.settingsState, tc.breakConfigFile)
Expand Down Expand Up @@ -543,7 +484,7 @@ func TestUpdateRegistryData(t *testing.T) {
ctx = wsl.WithMock(ctx, wslmock.New())
}

db, err := database.New(ctx, t.TempDir(), nil)
db, err := database.New(ctx, t.TempDir())
require.NoError(t, err, "Setup: could not create empty database")

_, dir := setUpMockSettings(t, ctx, db, tc.settingsState, tc.breakConfigFile)
Expand Down Expand Up @@ -646,7 +587,7 @@ func TestNotify(t *testing.T) {
ctx = wsl.WithMock(ctx, wslmock.New())
}

db, err := database.New(ctx, t.TempDir(), nil)
db, err := database.New(ctx, t.TempDir())
require.NoError(t, err, "Setup: could not create empty database")

_, dir := setUpMockSettings(t, ctx, db, untouched, false)
Expand Down
11 changes: 4 additions & 7 deletions windows-agent/internal/distros/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/canonical/ubuntu-pro-for-wsl/windows-agent/internal/consts"
"github.com/canonical/ubuntu-pro-for-wsl/windows-agent/internal/distros/distro"
"github.com/canonical/ubuntu-pro-for-wsl/windows-agent/internal/distros/worker"
log "github.com/canonical/ubuntu-pro-for-wsl/windows-agent/internal/grpc/logstreamer"
"gopkg.in/yaml.v3"
)
Expand All @@ -34,8 +33,7 @@ type DistroDB struct {

scheduleTrigger chan struct{}

storageDir string
provisioning worker.Provisioning
storageDir string

ctx context.Context
cancelCtx func()
Expand All @@ -57,7 +55,7 @@ type DistroDB struct {
// Every certain amount of times, the database wil purge all distros that
// are no longer registered or that have been marked as unreachable. This
// cleanup can be triggered on demmand with TriggerCleanup.
func New(ctx context.Context, storageDir string, provisioning worker.Provisioning) (*DistroDB, error) {
func New(ctx context.Context, storageDir string) (*DistroDB, error) {
if err := os.MkdirAll(storageDir, 0600); err != nil {
return nil, fmt.Errorf("could not create database directory: %w", err)
}
Expand All @@ -73,7 +71,6 @@ func New(ctx context.Context, storageDir string, provisioning worker.Provisionin
db := &DistroDB{
storageDir: storageDir,
scheduleTrigger: make(chan struct{}),
provisioning: provisioning,
ctx: ctx,
cancelCtx: cancel,
}
Expand Down Expand Up @@ -151,7 +148,7 @@ func (db *DistroDB) GetDistroAndUpdateProperties(ctx context.Context, name strin
if !found {
log.Debugf(ctx, "Cache miss, creating %q and adding it to the database", name)

d, err := distro.New(db.ctx, name, props, db.storageDir, &db.distroStartMu, distro.WithProvisioning(db.provisioning))
d, err := distro.New(db.ctx, name, props, db.storageDir, &db.distroStartMu)
if err != nil {
return nil, err
}
Expand All @@ -169,7 +166,7 @@ func (db *DistroDB) GetDistroAndUpdateProperties(ctx context.Context, name strin
go d.Cleanup(ctx)
delete(db.distros, normalizedName)

d, err := distro.New(db.ctx, name, props, db.storageDir, &db.distroStartMu, distro.WithProvisioning(db.provisioning))
d, err := distro.New(db.ctx, name, props, db.storageDir, &db.distroStartMu)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions windows-agent/internal/distros/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestNew(t *testing.T) {
databaseFromTemplate(t, dbDir, distroID{distro, guid})
}

db, err := database.New(ctx, dbDir, nil)
db, err := database.New(ctx, dbDir)
if err == nil {
defer db.Close(ctx)
}
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestDatabaseGetAll(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

db, err := database.New(ctx, t.TempDir(), nil)
db, err := database.New(ctx, t.TempDir())
require.NoError(t, err, "Setup: database creation should not fail")
defer db.Close(ctx)

Expand Down Expand Up @@ -166,7 +166,7 @@ func TestDatabaseGet(t *testing.T) {
distroID{registeredDistroInDB, registeredGUID},
distroID{nonRegisteredDistroInDB, oldGUID})

db, err := database.New(ctx, databaseDir, nil)
db, err := database.New(ctx, databaseDir)
require.NoError(t, err, "Setup: New() should return no error")

// Must use Cleanup. If we use defer, it'll run before the subtests are launched.
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestDatabaseGetAfterClose(t *testing.T) {
ctx = wsl.WithMock(ctx, wslmock.New())
}

db, err := database.New(ctx, t.TempDir(), nil)
db, err := database.New(ctx, t.TempDir())
require.NoError(t, err, "Setup: New() should return no error")

db.Close(ctx)
Expand Down Expand Up @@ -260,7 +260,7 @@ func TestDatabaseDump(t *testing.T) {
databaseFromTemplate(t, dbDir, distroID{distro1, guid1}, distroID{distro2, guid2})
}

db, err := database.New(ctx, dbDir, nil)
db, err := database.New(ctx, dbDir)
require.NoError(t, err, "Setup: empty database should be created without issue")
defer db.Close(ctx)

Expand Down Expand Up @@ -408,7 +408,7 @@ func TestGetDistroAndUpdateProperties(t *testing.T) {
distroID{distroInDB, guids[distroInDB]},
distroID{reRegisteredDistro, guids[reRegisteredDistro]})

db, err := database.New(ctx, dbDir, nil)
db, err := database.New(ctx, dbDir)
require.NoError(t, err, "Setup: New() should return no error")
defer db.Close(ctx)

Expand Down Expand Up @@ -510,7 +510,7 @@ func TestDatabaseCleanup(t *testing.T) {

databaseFromTemplate(t, dbDir, distros...)

db, err := database.New(ctx, dbDir, nil)
db, err := database.New(ctx, dbDir)
require.NoError(t, err, "Setup: New() should have returned no error")
defer db.Close(ctx)

Expand Down
17 changes: 4 additions & 13 deletions windows-agent/internal/distros/distro/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ func (*NotValidError) Error() string {

type options struct {
guid uuid.UUID
provisioning worker.Provisioning
taskProcessingContext context.Context
newWorkerFunc func(context.Context, *Distro, string, worker.Provisioning) (workerInterface, error)
newWorkerFunc func(context.Context, *Distro, string) (workerInterface, error)
}

// Option is an optional argument for distro.New.
Expand All @@ -75,14 +74,6 @@ func WithGUID(guid uuid.UUID) Option {
}
}

// WithProvisioning allows for providing a worker.Provisioning. If that is done,
// it'll be queried for the provisioning tasks and these will be submitted.
func WithProvisioning(c worker.Provisioning) Option {
return func(o *options) {
o.provisioning = c
}
}

// New creates a new Distro object after searching for a distro with the given name.
//
// - If identity.Name is not registered, a DistroDoesNotExist error is returned.
Expand All @@ -99,8 +90,8 @@ func New(ctx context.Context, name string, props Properties, storageDir string,
opts := options{
guid: nilGUID,
taskProcessingContext: context.Background(),
newWorkerFunc: func(ctx context.Context, d *Distro, dir string, provisioning worker.Provisioning) (workerInterface, error) {
return worker.New(ctx, d, dir, worker.WithProvisioning(provisioning))
newWorkerFunc: func(ctx context.Context, d *Distro, dir string) (workerInterface, error) {
return worker.New(ctx, d, dir)
},
}

Expand Down Expand Up @@ -147,7 +138,7 @@ func New(ctx context.Context, name string, props Properties, storageDir string,
return nil, err
}

distro.worker, err = opts.newWorkerFunc(opts.taskProcessingContext, distro, storageDir, opts.provisioning)
distro.worker, err = opts.newWorkerFunc(opts.taskProcessingContext, distro, storageDir)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 1bf2eb8

Please sign in to comment.