Skip to content

Commit

Permalink
Devmachine mage target (elastic#5777)
Browse files Browse the repository at this point in the history
* 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
pazone authored Oct 23, 2024
1 parent 49579ae commit 2241146
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 55 deletions.
3 changes: 3 additions & 0 deletions .tool-versions
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
86 changes: 43 additions & 43 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
136 changes: 136 additions & 0 deletions dev-tools/devmachine/devmachine.go
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
}
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ require (
golang.org/x/text v0.18.0
golang.org/x/time v0.6.0
golang.org/x/tools v0.24.0
google.golang.org/api v0.199.0
google.golang.org/grpc v1.67.1
google.golang.org/protobuf v1.34.2
gopkg.in/ini.v1 v1.67.0
Expand Down Expand Up @@ -130,8 +131,8 @@ require (
)

require (
cloud.google.com/go/auth v0.8.1 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.3 // indirect
cloud.google.com/go/auth v0.9.5 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.4 // indirect
cloud.google.com/go/compute/metadata v0.5.2 // indirect
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.13.0 // indirect
Expand Down Expand Up @@ -238,7 +239,7 @@ require (
github.com/google/s2a-go v0.1.8 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
github.com/googleapis/gax-go/v2 v2.13.0 // indirect
github.com/gophercloud/gophercloud v1.13.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
Expand Down Expand Up @@ -447,7 +448,6 @@ require (
golang.org/x/net v0.29.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
gonum.org/v1/gonum v0.15.1 // indirect
google.golang.org/api v0.192.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ cloud.google.com/go v0.56.0/go.mod h1:jr7tqZxxKOVYizybht9+26Z/gUq7tiRzu+ACVAMbKV
cloud.google.com/go v0.57.0/go.mod h1:oXiQ6Rzq3RAkkY7N6t3TcE6jE+CIBBbA36lwQ1JyzZs=
cloud.google.com/go v0.62.0/go.mod h1:jmCYTdRCQuc1PHIIJ/maLInMho30T/Y0M4hTdTShOYc=
cloud.google.com/go v0.65.0/go.mod h1:O5N8zS7uWy9vkA9vayVHs65eM1ubvY4h553ofrNHObY=
cloud.google.com/go/auth v0.8.1 h1:QZW9FjC5lZzN864p13YxvAtGUlQ+KgRL+8Sg45Z6vxo=
cloud.google.com/go/auth v0.8.1/go.mod h1:qGVp/Y3kDRSDZ5gFD/XPUfYQ9xW1iI7q8RIRoCyBbJc=
cloud.google.com/go/auth/oauth2adapt v0.2.3 h1:MlxF+Pd3OmSudg/b1yZ5lJwoXCEaeedAguodky1PcKI=
cloud.google.com/go/auth/oauth2adapt v0.2.3/go.mod h1:tMQXOfZzFuNuUxOypHlQEXgdfX5cuhwU+ffUuXRJE8I=
cloud.google.com/go/auth v0.9.5 h1:4CTn43Eynw40aFVr3GpPqsQponx2jv0BQpjvajsbbzw=
cloud.google.com/go/auth v0.9.5/go.mod h1:Xo0n7n66eHyOWWCnitop6870Ilwo3PiZyodVkkH1xWM=
cloud.google.com/go/auth/oauth2adapt v0.2.4 h1:0GWE/FUsXhf6C+jAkWgYm7X9tK8cuEIfy19DBn6B6bY=
cloud.google.com/go/auth/oauth2adapt v0.2.4/go.mod h1:jC/jOpwFP6JBxhB3P5Rr0a9HLMC/Pe3eaL4NmdvqPtc=
cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o=
cloud.google.com/go/bigquery v1.3.0/go.mod h1:PjpwJnslEMmckchkHFfq+HTD2DmtT67aNFKH1/VBDHE=
cloud.google.com/go/bigquery v1.4.0/go.mod h1:S8dzgnTigyfTmLBfrtrhyYhwRxG72rYxvftPBK2Dvzc=
Expand Down Expand Up @@ -516,8 +516,8 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/enterprise-certificate-proxy v0.3.2 h1:Vie5ybvEvT75RniqhfFxPRy3Bf7vr3h0cechB90XaQs=
github.com/googleapis/enterprise-certificate-proxy v0.3.2/go.mod h1:VLSiSSBs/ksPL8kq3OBOQ6WRI2QnaFynd1DCjZ62+V0=
github.com/googleapis/enterprise-certificate-proxy v0.3.4 h1:XYIDZApgAnrN1c855gTgghdIA6Stxb52D5RnLI1SLyw=
github.com/googleapis/enterprise-certificate-proxy v0.3.4/go.mod h1:YKe7cfqYXjKGpGvmSg28/fFvhNzinZQm8DGnaburhGA=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/gax-go/v2 v2.13.0 h1:yitjD5f7jQHhyDsnhKEBU52NdvvdSeGzlAnDPT0hH1s=
Expand Down Expand Up @@ -1698,8 +1698,8 @@ google.golang.org/api v0.24.0/go.mod h1:lIXQywCXRcnZPGlsd8NbLnOjtAoL6em04bJ9+z0M
google.golang.org/api v0.28.0/go.mod h1:lIXQywCXRcnZPGlsd8NbLnOjtAoL6em04bJ9+z0MncE=
google.golang.org/api v0.29.0/go.mod h1:Lcubydp8VUV7KeIHD9z2Bys/sm/vGKnG1UHuDBSrHWM=
google.golang.org/api v0.30.0/go.mod h1:QGmEvQ87FHZNiUVJkT14jQNYJ4ZJjdRF23ZXz5138Fc=
google.golang.org/api v0.192.0 h1:PljqpNAfZaaSpS+TnANfnNAXKdzHM/B9bKhwRlo7JP0=
google.golang.org/api v0.192.0/go.mod h1:9VcphjvAxPKLmSxVSzPlSRXy/5ARMEw5bf58WoVXafQ=
google.golang.org/api v0.199.0 h1:aWUXClp+VFJmqE0JPvpZOK3LDQMyFKYIow4etYd9qxs=
google.golang.org/api v0.199.0/go.mod h1:ohG4qSztDJmZdjK/Ar6MhbAmb/Rpi4JHOqagsh90K28=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
Expand Down
17 changes: 17 additions & 0 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/jedib0t/go-pretty/v6/table"
"github.com/otiai10/copy"

devmachine "github.com/elastic/elastic-agent/dev-tools/devmachine"
"github.com/elastic/elastic-agent/dev-tools/mage"
devtools "github.com/elastic/elastic-agent/dev-tools/mage"
"github.com/elastic/elastic-agent/dev-tools/mage/downloads"
Expand Down Expand Up @@ -157,6 +158,9 @@ type Integration mg.Namespace
// Otel namespace contains Open Telemetry related tasks.
type Otel mg.Namespace

// Devmachine namespace contains tasks related to remote development machines.
type Devmachine mg.Namespace

func CheckNoChanges() error {
fmt.Println(">> fmt - go run")
err := sh.RunV("go", "mod", "tidy", "-v")
Expand Down Expand Up @@ -844,6 +848,19 @@ func (Cloud) Push() error {
return nil
}

// Creates a new devmachine that will be auto-deleted in 6 hours.
// Example: MACHINE_IMAGE="family/platform-ingest-elastic-agent-ubuntu-2204" ZONE="us-central1-a" mage devmachine:create "pavel-dev-machine"
// ZONE defaults to 'us-central1-a', MACHINE_IMAGE defaults to 'family/platform-ingest-elastic-agent-ubuntu-2204'
func (Devmachine) Create(instanceName string) error {
if instanceName == "" {
return errors.New(
`instanceName is required.
Example:
mage devmachine:create "pavel-dev-machine" `)
}
return devmachine.Run(instanceName)
}

func Clean() {
mg.Deps(devtools.Clean, Build.Clean)
}
Expand Down

0 comments on commit 2241146

Please sign in to comment.