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 Mar 27, 2024
1 parent ef0a7e2 commit 6333fe0
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 242 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 @@ -13,8 +13,6 @@ import (

log "github.com/canonical/ubuntu-pro-for-wsl/common/grpc/logstreamer"
"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/ubuntu/decorate"
)

Expand Down Expand Up @@ -90,27 +88,6 @@ func (c *Config) Subscription() (token string, source Source, err error) {
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
s, err := c.get()
if err != nil {
return nil, fmt.Errorf("config: could not get provisioning tasks: %v", err)
}

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

// Landscape config
lconf, _ := s.Landscape.resolve()
taskList = append(taskList, tasks.LandscapeConfigure{Config: lconf, HostagentUID: s.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() (string, Source, error) {
Expand Down
76 changes: 8 additions & 68 deletions windows-agent/internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (

config "github.com/canonical/ubuntu-pro-for-wsl/windows-agent/internal/config"
"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 @@ -69,7 +67,6 @@ func TestSubscription(t *testing.T) {
"Error when the file cannot be read from": {settingsState: untouched, breakFile: true, wantError: true},
}

//nolint: dupl // This is mostly duplicate but de-duplicating with a meta-test worsens readability
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
Expand All @@ -78,7 +75,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, false)
Expand Down Expand Up @@ -124,7 +121,6 @@ func TestLandscapeConfig(t *testing.T) {
"Error when the file cannot be read from": {settingsState: untouched, breakFile: true, wantError: true},
}

//nolint: dupl // This is mostly duplicate but de-duplicating with a meta-test worsens readability
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
Expand All @@ -133,7 +129,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, false)
Expand Down Expand Up @@ -182,7 +178,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, false)
Expand Down Expand Up @@ -213,62 +209,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 {
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, 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 @@ -300,7 +240,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, tc.cannotWriteFile)
Expand Down Expand Up @@ -375,7 +315,7 @@ func TestSetStoreSubscription(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, tc.cannotWriteFile)
Expand Down Expand Up @@ -444,7 +384,7 @@ func TestSetUserLandscapeConfig(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, false)
Expand Down Expand Up @@ -509,7 +449,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, tc.cannotWriteFile)
Expand Down Expand Up @@ -578,7 +518,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, 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 @@ -17,7 +17,6 @@ import (
log "github.com/canonical/ubuntu-pro-for-wsl/common/grpc/logstreamer"
"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"
"github.com/ubuntu/decorate"
"gopkg.in/yaml.v3"
)
Expand All @@ -35,8 +34,7 @@ type DistroDB struct {

scheduleTrigger chan struct{}

storageDir string
provisioning worker.Provisioning
storageDir string

ctx context.Context
cancelCtx func()
Expand All @@ -58,7 +56,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) (db *DistroDB, err error) {
func New(ctx context.Context, storageDir string) (db *DistroDB, err error) {
defer decorate.OnError(&err, "could not initialize database")

select {
Expand All @@ -72,7 +70,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 @@ -150,7 +147,7 @@ func (db *DistroDB) GetDistroAndUpdateProperties(ctx context.Context, name strin
if !found {
log.Debugf(ctx, "Database: 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 @@ -168,7 +165,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 @@ -72,7 +72,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 @@ -114,7 +114,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 @@ -157,7 +157,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 @@ -202,7 +202,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 @@ -249,7 +249,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 @@ -396,7 +396,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 @@ -497,7 +497,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 @@ -56,9 +56,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 @@ -72,14 +71,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 @@ -96,8 +87,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 @@ -140,7 +131,7 @@ func New(ctx context.Context, name string, props Properties, storageDir string,
},
}

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 6333fe0

Please sign in to comment.