/
cloudfunctions.go
69 lines (55 loc) · 1.96 KB
/
cloudfunctions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cloudfunctions
import (
"context"
"fmt"
"google.golang.org/api/compute/v1"
"log"
)
// DeployMIG checks to see if a specified Managed Instance Group(MIG) is up and running. If it is running,
// it should shut it down, if it is unavailable it will restart it.
func DeployCloudRun() {
}
const ProjectID = "martins-project-2014113"
const InstanceTemplateName = "batch-mig-template"
const Zone = "us-central1-a"
const NetworkName = "default"
func DeployMIG() {
cs, err := InitComputeService()
if err != nil {
log.Fatal(err)
}
template, err := GetInstanceTemplate(cs)
if err != nil {
log.Fatal(err)
}
}
// InitComputeService obtains the compute service that allows us to use the compute API
func InitComputeService() (*compute.Service, error) {
ctx := context.Background()
return compute.NewService(ctx)
}
// Returns an instance template
func GetInstanceTemplate(computeService *compute.Service) (*compute.InstanceTemplate, error) {
get := computeService.InstanceTemplates.Get(ProjectID, InstanceTemplateName)
return get.Do(nil)
}
// StartMIG starts a managed instance group. This MIG will not have any instances.
func StartMIG(computeService *compute.Service, template *compute.InstanceTemplate) {
instanceGroup := compute.InstanceGroup{
Zone:Zone,
Description: "Instance Group for Random Batch Work",
Network: fmt.Sprintf("https://www.googleapis." +
"com/compute/v1/projects/%s/global/networks/%s", ProjectID, NetworkName),
NamedPorts: []*compute.NamedPort{{Name: "http", Port: 8080}},
}
createInstanceGroupCall := computeService.InstanceGroups.Insert(ProjectID, Zone, &instanceGroup)
_, err := createInstanceGroupCall.Do()
if err != nil {
log.Fatal(err)
}
computeService.InstanceGroupManagers.SetInstanceTemplate(ProjectID, Zone, )
computeService.InstanceGroupManagers.Insert(ProjectID, Zone, )
}
// ApplyInstanceTemplateToMIG will apply an instance template to a Managed Instance Group
func ApplyInstanceTemplateToMIG(instanceGroupName string) {
}