-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Azure support #54
base: master
Are you sure you want to change the base?
Azure support #54
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,7 @@ vendor | |
*.bak | ||
*.out | ||
*.db | ||
*.env | ||
*.env | ||
*# | ||
*.#* | ||
*~ | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,3 +108,11 @@ | |
[[constraint]] | ||
name = "github.com/lib/pq" | ||
version = "1.0.0" | ||
|
||
[[constraint]] | ||
name = "github.com/Azure/azure-sdk-for-go" | ||
version = "32.5.0" | ||
|
||
[[override]] | ||
name = "github.com/Azure/go-autorest" | ||
version = "13.0.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. K will move to go.mod |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,116 @@ | |
// that can be found in the LICENSE file. | ||
|
||
package azure | ||
|
||
import ( | ||
"context" | ||
"github.com/drone/autoscaler" | ||
"github.com/rs/zerolog/log" | ||
"fmt" | ||
"time" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. best practice for import ordering https://github.com/golang/go/wiki/CodeReviewComments#imports |
||
|
||
"bytes" | ||
"encoding/base64" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute" | ||
"github.com/Azure/go-autorest/autorest/to" | ||
) | ||
|
||
func (p *provider) computeMachineRequest(ctx context.Context, base string, opts autoscaler.InstanceCreateOpts) (compute.VirtualMachine, *string, error) { | ||
buf := new(bytes.Buffer) | ||
err := p.userdata.Execute(buf, &opts) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unhandled exception |
||
cloudInit := base64.StdEncoding.EncodeToString(buf.Bytes()) | ||
|
||
nic, ip, err := p.CreateNIC(ctx, p.vnet, p.subnet, p.nsg, base+"-ip", base+"-nic") | ||
if err != nil { | ||
return compute.VirtualMachine{}, nil, err | ||
} | ||
|
||
return compute.VirtualMachine{ | ||
Location: to.StringPtr(p.location), | ||
VirtualMachineProperties: &compute.VirtualMachineProperties{ | ||
HardwareProfile: &compute.HardwareProfile{ | ||
VMSize: p.vmSize, | ||
}, | ||
StorageProfile: &compute.StorageProfile{ | ||
OsDisk: &compute.OSDisk{ | ||
Name: to.StringPtr(base + "-" + "osdisk"), | ||
CreateOption: compute.DiskCreateOptionTypesFromImage, | ||
DiskSizeGB: to.Int32Ptr(p.volumeSize), | ||
}, | ||
ImageReference: &compute.ImageReference{ | ||
Offer: to.StringPtr(p.imageOffer), | ||
Publisher: to.StringPtr(p.imagePublisher), | ||
Sku: to.StringPtr(p.imageSKU), | ||
Version: to.StringPtr(p.imageVersion), | ||
}, | ||
}, | ||
NetworkProfile: &compute.NetworkProfile{ | ||
NetworkInterfaces: &[]compute.NetworkInterfaceReference{ | ||
{ | ||
ID: nic.ID, | ||
NetworkInterfaceReferenceProperties: &compute.NetworkInterfaceReferenceProperties{ | ||
Primary: to.BoolPtr(true), | ||
}, | ||
}, | ||
}, | ||
}, | ||
OsProfile: &compute.OSProfile{ | ||
ComputerName: to.StringPtr(base + "-" + "vm"), | ||
AdminUsername: to.StringPtr(p.adminUsername), | ||
AdminPassword: to.StringPtr(p.adminPassword), | ||
CustomData: &cloudInit, | ||
}, | ||
}, | ||
}, ip.PublicIPAddressPropertiesFormat.IPAddress, nil | ||
} | ||
|
||
func (p *provider) Create(ctx context.Context, opts autoscaler.InstanceCreateOpts) (*autoscaler.Instance, error) { | ||
logger := log.Ctx(ctx) | ||
logger.Debug().Msg("Azure creating an instance.") | ||
|
||
client, err := p.getClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
base := opts.Name | ||
computeRequest, ipaddress, err := p.computeMachineRequest(ctx, base, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
future, err := client.CreateOrUpdate( | ||
ctx, | ||
p.resourceGroup, | ||
base+"-vm", | ||
computeRequest, | ||
) | ||
|
||
err = future.WaitForCompletionRef(ctx, client.Client) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
azureInstance, err := future.Result(client) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unhandled error |
||
client.Start(ctx, p.resourceGroup, p.vmName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does start return any parameters? if it returns an error it needs to be handled |
||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fmt.Printf("Starting sleep: %v\n", time.Now().Unix()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use logger instead of |
||
time.Sleep(180 * time.Second) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should probably alter this to just poll on the static IP I provide until it's available. This was a quick hack to fix an issue I had noticed around an agent getting assigned at times +1 builds. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes this should use polling, which is how we handle this for the other providers. You can use this example as reference https://github.com/drone/autoscaler/blob/master/drivers/amazon/create.go#L122:L175 |
||
fmt.Printf("Ending sleep: %v\n", time.Now().Unix()) | ||
|
||
instance := &autoscaler.Instance{ | ||
Provider: autoscaler.ProviderAzure, | ||
Address: *ipaddress, | ||
ID: base, // Set it to name because required for deallocating. | ||
Size: string(azureInstance.HardwareProfile.VMSize), | ||
Region: *azureInstance.Location, | ||
Image: *azureInstance.StorageProfile.ImageReference.Sku, | ||
} | ||
|
||
return instance, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what are these lines for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Emacs backup files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these should be removed and added to your global .gitignore file or in
.git/info/exclude