forked from elastic/elastic-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Devmachine mage target (elastic#5777)
* Devmachine mage target * Docs * Merged * Fix tools versions format * Fixed lint issues * Fix lint issues * Added licence headers * Applied requested changes * Applied code review tips * Added required labels * Added strings to constants * Moved ZONE and MACHINE images to optional env variables
- Loading branch information
Showing
6 changed files
with
211 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mage 1.14.0 | ||
golang 1.22.8 | ||
terraform 1.9.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15948,6 +15948,43 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
|
||
-------------------------------------------------------------------------------- | ||
Dependency : google.golang.org/api | ||
Version: v0.199.0 | ||
Licence type (autodetected): BSD-3-Clause | ||
-------------------------------------------------------------------------------- | ||
|
||
Contents of probable licence file $GOMODCACHE/google.golang.org/[email protected]/LICENSE: | ||
|
||
Copyright (c) 2011 Google Inc. All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are | ||
met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following disclaimer | ||
in the documentation and/or other materials provided with the | ||
distribution. | ||
* Neither the name of Google Inc. nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
|
||
-------------------------------------------------------------------------------- | ||
Dependency : google.golang.org/grpc | ||
Version: v1.67.1 | ||
|
@@ -18778,11 +18815,11 @@ Indirect dependencies | |
|
||
-------------------------------------------------------------------------------- | ||
Dependency : cloud.google.com/go/auth | ||
Version: v0.8.1 | ||
Version: v0.9.5 | ||
Licence type (autodetected): Apache-2.0 | ||
-------------------------------------------------------------------------------- | ||
|
||
Contents of probable licence file $GOMODCACHE/cloud.google.com/go/auth@v0.8.1/LICENSE: | ||
Contents of probable licence file $GOMODCACHE/cloud.google.com/go/auth@v0.9.5/LICENSE: | ||
|
||
|
||
Apache License | ||
|
@@ -18990,11 +19027,11 @@ Contents of probable licence file $GOMODCACHE/cloud.google.com/go/[email protected]/LI | |
|
||
-------------------------------------------------------------------------------- | ||
Dependency : cloud.google.com/go/auth/oauth2adapt | ||
Version: v0.2.3 | ||
Version: v0.2.4 | ||
Licence type (autodetected): Apache-2.0 | ||
-------------------------------------------------------------------------------- | ||
|
||
Contents of probable licence file $GOMODCACHE/cloud.google.com/go/auth/[email protected].3/LICENSE: | ||
Contents of probable licence file $GOMODCACHE/cloud.google.com/go/auth/[email protected].4/LICENSE: | ||
|
||
|
||
Apache License | ||
|
@@ -33761,11 +33798,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
|
||
-------------------------------------------------------------------------------- | ||
Dependency : github.com/googleapis/enterprise-certificate-proxy | ||
Version: v0.3.2 | ||
Version: v0.3.4 | ||
Licence type (autodetected): Apache-2.0 | ||
-------------------------------------------------------------------------------- | ||
|
||
Contents of probable licence file $GOMODCACHE/github.com/googleapis/[email protected].2/LICENSE: | ||
Contents of probable licence file $GOMODCACHE/github.com/googleapis/[email protected].4/LICENSE: | ||
|
||
|
||
Apache License | ||
|
@@ -73362,43 +73399,6 @@ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
-------------------------------------------------------------------------------- | ||
Dependency : google.golang.org/api | ||
Version: v0.192.0 | ||
Licence type (autodetected): BSD-3-Clause | ||
-------------------------------------------------------------------------------- | ||
|
||
Contents of probable licence file $GOMODCACHE/google.golang.org/[email protected]/LICENSE: | ||
|
||
Copyright (c) 2011 Google Inc. All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are | ||
met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following disclaimer | ||
in the documentation and/or other materials provided with the | ||
distribution. | ||
* Neither the name of Google Inc. nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
|
||
-------------------------------------------------------------------------------- | ||
Dependency : google.golang.org/genproto/googleapis/api | ||
Version: v0.0.0-20240903143218-8af14fe29dc1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License 2.0; | ||
// you may not use this file except in compliance with the Elastic License 2.0. | ||
|
||
package devmachine | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
compute "google.golang.org/api/compute/v1" | ||
) | ||
|
||
const ( | ||
ZONE_TEMPLATE = "zones/%s/machineTypes/n1-standard-4" | ||
IMAGE_TEMPLATE = "projects/%s/global/images/%s" | ||
DEFAULT_IMAGE = "family/platform-ingest-elastic-agent-ubuntu-2204" | ||
DEFAULT_ZONE = "us-central1-a" | ||
) | ||
|
||
func Run(instanceName string) error { | ||
machineImage := os.Getenv("MACHINE_IMAGE") | ||
if machineImage == "" { | ||
machineImage = DEFAULT_IMAGE | ||
} | ||
zone := os.Getenv("ZONE") | ||
if zone == "" { | ||
zone = DEFAULT_ZONE | ||
} | ||
|
||
ctx := context.Background() | ||
log.Println(">> Creating devmachine") | ||
projectID := "elastic-platform-ingest" | ||
log.Println("Authenticating with GCP...") | ||
computeService, err := compute.NewService(ctx) | ||
if err != nil { | ||
log.Fatalf("Failed to create compute service: %v. \nRun `gcloud auth login` to make it work", err) | ||
} | ||
|
||
imageProject := "elastic-images-prod" | ||
machineType := fmt.Sprintf(ZONE_TEMPLATE, zone) | ||
sourceImage := fmt.Sprintf(IMAGE_TEMPLATE, imageProject, machineImage) | ||
|
||
instance := &compute.Instance{ | ||
Name: instanceName, | ||
MachineType: machineType, | ||
Disks: []*compute.AttachedDisk{ | ||
{ | ||
AutoDelete: true, | ||
Boot: true, | ||
InitializeParams: &compute.AttachedDiskInitializeParams{ | ||
SourceImage: sourceImage, | ||
}, | ||
}, | ||
}, | ||
NetworkInterfaces: []*compute.NetworkInterface{ | ||
{ | ||
AccessConfigs: []*compute.AccessConfig{ | ||
{ | ||
Name: "External NAT", | ||
Type: "ONE_TO_ONE_NAT", | ||
}, | ||
}, | ||
Network: "global/networks/default", // Use default network | ||
}, | ||
}, | ||
Labels: map[string]string{ | ||
"division": "engineering", | ||
"org": "platform-ingest", | ||
"project": "elastic-agent", | ||
"team": "elastic-agent", | ||
"max-age": "6h", | ||
}, | ||
Scheduling: &compute.Scheduling{ | ||
MaxRunDuration: &compute.Duration{ | ||
Seconds: 6 * 60 * 60, // 6 hours | ||
Nanos: 0, | ||
}, | ||
InstanceTerminationAction: "DELETE", | ||
}, | ||
} | ||
|
||
op, err := computeService.Instances.Insert(projectID, zone, instance).Context(ctx).Do() | ||
if err != nil { | ||
log.Fatalf("Failed to create instance: %v", err) | ||
} | ||
|
||
log.Printf("Instance creation initiated: %s\n", op.Name) | ||
log.Printf("Instance name: %s\n", instanceName) | ||
|
||
retriesLimit := 24 | ||
retryCount := 0 | ||
for { | ||
op, err = computeService.ZoneOperations.Get(projectID, zone, op.Name).Context(ctx).Do() | ||
if err != nil { | ||
log.Fatalf("Failed to get operation status: %v", err) | ||
} | ||
if op.Status != "DONE" { | ||
log.Print(".") | ||
} | ||
if op.Status == "DONE" { | ||
log.Println("Instance creation complete!") | ||
break | ||
} | ||
if retryCount == retriesLimit { | ||
log.Fatalf("Instance creation failed after %d retries", retriesLimit) | ||
} | ||
time.Sleep(5 * time.Second) | ||
retryCount++ | ||
} | ||
|
||
instanceDetails, err := computeService.Instances.Get(projectID, zone, instanceName).Context(ctx).Do() | ||
if err != nil { | ||
log.Fatalf("Failed to get instance details: %v", err) | ||
return err | ||
} | ||
|
||
log.Printf("Instance created: %s\n", instanceDetails.Name) | ||
|
||
log.Printf( | ||
`SSH into your instance using: | ||
gcloud compute ssh --zone "%s" buildkite-agent@%s --project "elastic-platform-ingest" | ||
Once you are in the instance, type "bash" to start a bash shell. | ||
`, zone, instanceName) | ||
|
||
log.Printf( | ||
`Copy files to your instance using: | ||
gcloud compute scp --recurse --compress --zone "%s" buildkite-agent%s:~ [your_file/your_current_dir] --project "elastic-platform-ingest" | ||
TIP: Better use git on the remote machine. | ||
`, zone, instanceName) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters