Skip to content

Commit

Permalink
supporting .pem files
Browse files Browse the repository at this point in the history
  • Loading branch information
aronchick committed Oct 13, 2024
1 parent 6a994f1 commit 453910c
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 103 deletions.
1 change: 1 addition & 0 deletions .cspell/custom-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ predeclared
presignedurldisabled
presignedurlexpiration
privateip
privkey
probeexec
probehttp
Progammer
Expand Down
37 changes: 0 additions & 37 deletions cmd/beta/azure/create_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,6 @@ func runDeployment(ctx context.Context, azureProvider *azure.AzureProvider) erro
return err
}

if err := provisionMachines(ctx, azureProvider, m); err != nil {
return err
}

if err := provisionBacalhauCluster(ctx, azureProvider, m); err != nil {
return err
}
Expand Down Expand Up @@ -241,39 +237,6 @@ func createResources(
return nil
}

func provisionMachines(
ctx context.Context,
azureProvider *azure.AzureProvider,
m *display.DisplayModel,
) error {
var wg sync.WaitGroup
errChan := make(chan error, len(m.Deployment.Machines))

for _, machine := range m.Deployment.Machines {
wg.Add(1)
go func(machineName string) {
defer wg.Done()
err := azureProvider.GetClusterDeployer().ProvisionPackagesOnMachine(ctx, machineName)
if err != nil {
errChan <- fmt.Errorf("failed to provision machine %s: %w", machineName, err)
return
}
go updateMachineConfig(m.Deployment, machineName)
}(machine.GetName())
}

wg.Wait()
close(errChan)

for err := range errChan {
if err != nil {
return err
}
}

return nil
}

func provisionBacalhauCluster(
ctx context.Context,
azureProvider *azure.AzureProvider,
Expand Down
41 changes: 0 additions & 41 deletions cmd/beta/gcp/create_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gcp
import (
"context"
"fmt"
"sync"
"time"

"github.com/bacalhau-project/andaime/pkg/display"
Expand Down Expand Up @@ -186,10 +185,6 @@ func runDeployment(ctx context.Context, gcpProvider *gcp_provider.GCPProvider) e
return err
}

if err := provisionMachines(ctx, gcpProvider, m); err != nil {
return err
}

if err := provisionBacalhauCluster(ctx, gcpProvider, m); err != nil {
return err
}
Expand Down Expand Up @@ -243,42 +238,6 @@ func createResources(
return nil
}

func provisionMachines(
ctx context.Context,
gcpProvider *gcp_provider.GCPProvider,
m *display.DisplayModel,
) error {
var wg sync.WaitGroup
errChan := make(chan error, len(m.Deployment.Machines))

for _, machine := range m.Deployment.Machines {
wg.Add(1)
go func(machineName string) {
defer wg.Done()
err := gcpProvider.GetClusterDeployer().ProvisionPackagesOnMachine(ctx, machineName)
if err != nil {
errChan <- fmt.Errorf("failed to provision machine %s: %w", machineName, err)
return
}
machine := m.Deployment.GetMachine(machineName)
machine.SetServiceState(models.ServiceTypeSSH.Name, models.ServiceStateSucceeded)
machine.SetServiceState(models.ServiceTypeDocker.Name, models.ServiceStateSucceeded)
updateMachineConfig(m.Deployment, machineName)
}(machine.GetName())
}

wg.Wait()
close(errChan)

for err := range errChan {
if err != nil {
return err
}
}

return nil
}

func provisionBacalhauCluster(
ctx context.Context,
gcpProvider *gcp_provider.GCPProvider,
Expand Down
7 changes: 5 additions & 2 deletions pkg/models/interfaces/common/cluster_deployerer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import (
)

type ClusterDeployerer interface {
ProvisionMachine(
ctx context.Context,
sshConfig sshutils.SSHConfiger,
machine models.Machiner,
) error
WaitForAllMachinesToReachState(
ctx context.Context,
resourceType string,
state models.MachineResourceState,
) error

// ProvisionAllMachinesWithPackages(ctx context.Context) error
ProvisionPackagesOnMachine(ctx context.Context, machineName string) error
ExecuteCustomScript(
ctx context.Context,
sshConfig sshutils.SSHConfiger,
Expand Down
114 changes: 107 additions & 7 deletions pkg/providers/azure/deploy_bacalhau_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,27 @@ func (s *PkgProvidersAzureDeployBacalhauTestSuite) TestDeployBacalhauNode() {
nodeType: "requester",
sshBehavior: sshutils.ExpectedSSHBehavior{
PushFileExpectations: []sshutils.PushFileExpectation{
{Dst: mock.Anything, Executable: true, Error: nil, Times: 3},
{Dst: mock.Anything, Executable: true, Error: nil, Times: 6},
},
ExecuteCommandExpectations: []sshutils.ExecuteCommandExpectation{
{
Cmd: "sudo /tmp/install-docker.sh",
Output: "",
Error: nil,
Times: 1,
},
{
Cmd: "sudo docker run hello-world",
Output: "Hello from Docker!",
Error: nil,
Times: 1,
},
{
Cmd: "sudo /tmp/install-core-packages.sh",
Output: "",
Error: nil,
Times: 1,
},
{Cmd: mock.Anything, Output: "", Error: nil, Times: 3},
{
Cmd: "bacalhau node list --output json --api-host 0.0.0.0",
Expand Down Expand Up @@ -367,7 +385,7 @@ func (s *PkgProvidersAzureDeployBacalhauTestSuite) TestDeployBacalhauNode() {
NodeType: models.BacalhauNodeTypeOrchestrator,
},
},
expectedError: "failed to push node config metadata script",
expectedError: "push file error",
},
{
name: "Successful worker deployment",
Expand All @@ -378,10 +396,28 @@ func (s *PkgProvidersAzureDeployBacalhauTestSuite) TestDeployBacalhauNode() {
Dst: mock.Anything,
Executable: true,
Error: nil,
Times: 3,
Times: 5,
},
},
ExecuteCommandExpectations: []sshutils.ExecuteCommandExpectation{
{
Cmd: "sudo /tmp/install-docker.sh",
Output: "",
Error: nil,
Times: 2,
},
{
Cmd: "sudo docker run hello-world",
Output: "Hello from Docker!",
Error: nil,
Times: 2,
},
{
Cmd: "sudo /tmp/install-core-packages.sh",
Output: "",
Error: nil,
Times: 2,
},
{Cmd: mock.Anything, Output: "", Error: nil, Times: 3},
{
Cmd: "bacalhau node list --output json --api-host 1.2.3.4",
Expand Down Expand Up @@ -451,6 +487,14 @@ func (s *PkgProvidersAzureDeployBacalhauTestSuite) TestDeployOrchestrator() {
nodeType := models.BacalhauNodeTypeOrchestrator

expectedLines := map[string][]string{
"install-docker.sh": {
"sudo apt-get update",
"sudo apt-get install -y docker.io",
},
"install-core-packages.sh": {
"sudo apt-get update && \\",
"sudo apt-get install -y sudo",
},
"get-node-config-metadata.sh": {
`cat << EOF > /etc/node-config`,
fmt.Sprintf(`MACHINE_TYPE="%s"`, vmSize),
Expand Down Expand Up @@ -484,14 +528,20 @@ func (s *PkgProvidersAzureDeployBacalhauTestSuite) TestDeployOrchestrator() {
})

sshBehavior := sshutils.ExpectedSSHBehavior{
PushFileExpectations: []sshutils.PushFileExpectation{}, // Not setting behaviors here because we want to get the script being pushed
PushFileExpectations: []sshutils.PushFileExpectation{}, // We aren't setting push files here - it's down below
ExecuteCommandExpectations: []sshutils.ExecuteCommandExpectation{
{
Cmd: "sudo docker run hello-world",
Output: "Hello from Docker!",
Error: nil,
Times: 1,
},
{
Cmd: mock.Anything,
ProgressCallback: mock.Anything,
Output: "",
Error: nil,
Times: 3,
Times: 5,
},
{
Cmd: fmt.Sprintf(
Expand Down Expand Up @@ -525,6 +575,18 @@ func (s *PkgProvidersAzureDeployBacalhauTestSuite) TestDeployOrchestrator() {
s.mockSSHConfig.On("WaitForSSH", s.ctx, mock.Anything, mock.Anything).Return(nil).Once()

renderedScripts := make(map[string][]byte)
s.mockSSHConfig.On("PushFile", s.ctx, "/tmp/install-docker.sh", mock.Anything, true, mock.Anything).
Run(func(args mock.Arguments) {
renderedScripts["install-docker.sh"] = args.Get(2).([]byte)
}).
Return(nil).
Once()
s.mockSSHConfig.On("PushFile", s.ctx, "/tmp/install-core-packages.sh", mock.Anything, true, mock.Anything).
Run(func(args mock.Arguments) {
renderedScripts["install-core-packages.sh"] = args.Get(2).([]byte)
}).
Return(nil).
Once()
s.mockSSHConfig.On("PushFile", s.ctx, "/tmp/get-node-config-metadata.sh", mock.Anything, true, mock.Anything).
Run(func(args mock.Arguments) {
renderedScripts["get-node-config-metadata.sh"] = args.Get(2).([]byte)
Expand All @@ -544,6 +606,10 @@ func (s *PkgProvidersAzureDeployBacalhauTestSuite) TestDeployOrchestrator() {
Return(nil).
Once()

sshutils.NewSSHConfigFunc = func(host string, port int, user string, sshPrivateKeyPath string) (sshutils.SSHConfiger, error) {
return s.mockSSHConfig, nil
}

err := s.deployer.ProvisionOrchestrator(s.ctx, "orch")

s.NoError(err)
Expand All @@ -554,6 +620,7 @@ func (s *PkgProvidersAzureDeployBacalhauTestSuite) TestDeployOrchestrator() {
s.NotEmpty(s.deployment.OrchestratorIP)

filesToTest := map[string]string{
"install-docker.sh": fileToTestInstallDocker,
"get-node-config-metadata.sh": fmt.Sprintf(fileToTestMetadata, location, ip, orchestrators),
"install-bacalhau.sh": fileToTestInstall,
"install-run-bacalhau.sh": fileToTestServe,
Expand All @@ -568,23 +635,46 @@ func (s *PkgProvidersAzureDeployBacalhauTestSuite) TestDeployOrchestrator() {
func (s *PkgProvidersAzureDeployBacalhauTestSuite) TestDeployWorkers() {
s.SetupTest()
machines := map[string]models.Machiner{
"orch": &models.Machine{Name: "orch", Orchestrator: true, PublicIP: "1.2.3.4"},
"orch": &models.Machine{
Name: "orch",
Orchestrator: true,
PublicIP: "1.2.3.4",
NodeType: models.BacalhauNodeTypeOrchestrator,
},
"worker1": &models.Machine{
Name: "worker1",
Orchestrator: false,
PublicIP: "2.3.4.5",
OrchestratorIP: "1.2.3.4",
NodeType: models.BacalhauNodeTypeCompute,
},
"worker2": &models.Machine{
Name: "worker2",
Orchestrator: false,
PublicIP: "3.4.5.6",
OrchestratorIP: "1.2.3.4",
NodeType: models.BacalhauNodeTypeCompute,
},
}

sshBehavior := sshutils.ExpectedSSHBehavior{
PushFileExpectations: []sshutils.PushFileExpectation{
{
Dst: "/tmp/install-docker.sh",
FileContents: []byte(""),
Executable: true,
ProgressCallback: mock.Anything,
Error: nil,
Times: 3,
},
{
Dst: "/tmp/install-core-packages.sh",
FileContents: []byte(""),
Executable: true,
ProgressCallback: mock.Anything,
Error: nil,
Times: 3,
},
{
Dst: "/tmp/get-node-config-metadata.sh",
FileContents: []byte(""),
Expand All @@ -611,6 +701,13 @@ func (s *PkgProvidersAzureDeployBacalhauTestSuite) TestDeployWorkers() {
},
},
ExecuteCommandExpectations: []sshutils.ExecuteCommandExpectation{
{
Cmd: "sudo docker run hello-world",
ProgressCallback: mock.Anything,
Output: "Hello from Docker!",
Error: nil,
Times: 3,
},
{
Cmd: "bacalhau node list --output json --api-host 1.2.3.4",
ProgressCallback: mock.Anything,
Expand All @@ -636,7 +733,7 @@ func (s *PkgProvidersAzureDeployBacalhauTestSuite) TestDeployWorkers() {
ProgressCallback: mock.Anything,
Output: "",
Error: nil,
Times: 9,
Times: 15,
},
},
InstallSystemdServiceExpectation: &sshutils.Expectation{
Expand Down Expand Up @@ -688,6 +785,9 @@ TOKEN=""
NODE_TYPE="requester"
`

const fileToTestInstallDocker = `sudo apt-get update
sudo apt-get install -y docker.io`

const fileToTestInstall = `sudo curl -sSL https://get.bacalhau.org/install.sh?dl="${BACALHAU_INSTALL_ID}" | sudo bash`

const fileToTestServe = `/usr/local/bin/bacalhau serve \`
2 changes: 1 addition & 1 deletion pkg/providers/azure/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (s *PkgProvidersAzureIntegrationTest) TestProvisionResourcesSuccess() {
s.provider.SetClusterDeployer(s.clusterDeployer)
m := display.GetGlobalModelFunc()
for _, machine := range m.Deployment.Machines {
err := s.provider.GetClusterDeployer().ProvisionPackagesOnMachine(ctx, machine.GetName())
err := s.provider.GetClusterDeployer().ProvisionMachine(ctx, s.mockSSHConfig, machine)
s.Require().NoError(err)
}

Expand Down
Loading

0 comments on commit 453910c

Please sign in to comment.