Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 6df9093

Browse files
FrenchBenDavid Chung
authored andcommitted
Updates to the GCP docs and logging (#758)
Signed-off-by: French Ben <[email protected]>
1 parent b6cbd59 commit 6df9093

File tree

9 files changed

+147
-37
lines changed

9 files changed

+147
-37
lines changed

docs/playbooks/intro/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ If you don't have infrakit or go compiler installed locally, just
88

99
```shell
1010

11-
docker run --rm -v `pwd`:/build infrakit/installer build-infrakit darwin
11+
docker run --rm -v `pwd`:/build -e GOARCH=amd64 -e GOOS=darwin infrakit/installer build-infrakit
1212
sudo cp ./infrakit /usr/local/bin
1313
```
1414
This will cross-compile the `infrakit` cli for Mac OSX. For Linux, there's no need to set the `GOOS` and `GOARCH`
@@ -17,7 +17,7 @@ environment variables.
1717
### Linux
1818

1919
```shell
20-
$ docker run --rm -v `pwd`:/build infrakit/installer build-infrakit linux
20+
$ docker run --rm -v `pwd`:/build -e GOARCH=amd64 -e GOOS=linux infrakit/installer build-infrakit
2121
$ sudo cp ./infrakit /usr/local/bin/
2222
```
2323

docs/playbooks/intro/gcp/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,78 @@
11
Running InfraKit on GCP
22
=======================
33

4+
The `infrakit` cli is a dynamic command line tool. You can add 'playbooks' which contain pre-built features.
5+
This directory is in fact a playbook (see `index.ikb`). To add this playbook, do this:
6+
7+
```shell
8+
9+
infrakit playbook add gcp https://docker.github.io/infrakit/playbooks/intro/gcp/index.ikb
10+
```
11+
12+
Verify that the playbook `gcp` has been added:
13+
14+
```shell
15+
16+
infrakit playbook gcp -h
17+
18+
19+
___ ________ ________ ________ ________ ___ __ ___ _________
20+
|\ \|\ ___ \|\ _____\\ __ \|\ __ \|\ \|\ \ |\ \|\___ ___\
21+
\ \ \ \ \\ \ \ \ \__/\ \ \|\ \ \ \|\ \ \ \/ /|\ \ \|___ \ \_|
22+
\ \ \ \ \\ \ \ \ __\\ \ _ _\ \ __ \ \ ___ \ \ \ \ \ \
23+
\ \ \ \ \\ \ \ \ \_| \ \ \\ \\ \ \ \ \ \ \\ \ \ \ \ \ \ \
24+
\ \__\ \__\\ \__\ \__\ \ \__\\ _\\ \__\ \__\ \__\\ \__\ \__\ \ \__\
25+
\|__|\|__| \|__|\|__| \|__|\|__|\|__|\|__|\|__| \|__|\|__| \|__|
26+
27+
28+
gcp
29+
30+
Usage:
31+
infrakit playbook gcp [command]
32+
33+
Available Commands:
34+
provision-instance provision-instance
35+
start-daemon start-daemon
36+
37+
...
38+
39+
Use "infrakit playbook gcp [command] --help" for more information about a command.
40+
```
41+
42+
## Prerequisites:
43+
44+
This tutorial uses Google Cloud Platform. Be sure you have the API token; you will be prompted to provide it
45+
if you don't provide it in the command line flag. You can also authenticate via the `gcloud` tool and thus
46+
have a `~/.config/gcloud/application_default_credentials.json` file which has the access token.
47+
48+
## Start up Infrakit controller daemons and plugins
49+
50+
The CLI will guide you through starting up infrakit controllers if command line flags were not given.
51+
In the example below, we start up only the Google Cloud Platform plugin along with the rest of Infrakit controllers.
52+
All the controllers and plugins are running as Docker containers:
53+
54+
```shell
55+
56+
$ infrakit playbook gcp start-daemon
57+
Credentials JSON path? [/home/frenchben/.config/gcloud/application_default_credentials.json]:
58+
What's the zone? : us-west1-a
59+
What's the name of the project? : my-gcp-project
60+
Starting daemon
61+
Tailing log
62+
63+
```
64+
65+
## Provision an Instance
66+
67+
To provision an instance on GCP, use the flags or follow the prompts:
68+
69+
```shell
70+
71+
$ infrakit playbook gcp provision-instance
72+
Owner? [frenchben]:
73+
Image to boot? [https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1404-trusty-v20161205]:
74+
Machine type? [n1-standard-1]:
75+
Private IP address (IPv4)? [10.128.0.10]:
76+
Disk size in GB? [100]:
77+
78+
```

docs/playbooks/intro/gcp/provision-instance.ikt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{{/* Input to create instance using the GCP instance plugin */}}
2-
{{/* =% instanceProvision "instance-gcp" true %= */}}
2+
{{/* =% instanceProvision "google/compute" true %= */}}
33

44
{{ $user := flag "user" "string" "owner" | prompt "Owner?" "string" (env "USER") nil }}
55
{{ $image := flag "image-id" "string" "Image ID" | prompt "Image to boot?" "string" "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1404-trusty-v20161205" }}
66
{{ $instanceType := flag "instance-type" "string" "Machine type" | prompt "Machine type?" "string" "n1-standard-1"}}
77
{{ $privateIP := flag "private-ip" "string" "Private IP" | prompt "Private IP address (IPv4)?" "string" "10.128.0.10" nil }}
88

9-
{{ $diskSize := flag "disk-size" "int" "Disk size in mb" | prompt "Disk size in MB?" "int" 100 }}
9+
{{ $diskSize := flag "disk-size" "int" "Disk size in gb" | prompt "Disk size in GB?" "int" 100 }}
1010

1111

1212
LogicalID : {{ $privateIP }}
@@ -26,8 +26,9 @@ Properties:
2626
Description: Some description
2727
Network: default
2828
MachineType: {{ $instanceType }}
29-
DiskSizeMb: {{ $diskSize }}
30-
DiskImage: {{ $image }}
29+
Disks:
30+
- Image: {{ $image }}
31+
SizeGb: {{ $diskSize }}
3132
Scopes:
3233
- https://www.googleapis.com/auth/cloudruntimeconfig
3334
- https://www.googleapis.com/auth/logging.write

docs/playbooks/intro/gcp/start-plugin-daemon.ikt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
{{ $gcpCredentials := (cat $credentials ":/infrakit/platforms/gcp/credentials.json" | nospace) }}
1111

1212
echo "Starting daemon"
13-
infrakit-instance-gcp \
14-
--namespace-tags {{cat "infrakit.scope=" $project | nospace}} \
15-
--zone {{ $zone }} --log 5 --project {{ $project }} \
13+
INFRAKIT_GOOGLE_ZONE={{ $zone }} \
14+
INFRAKIT_GOOGLE_PROJECT={{ $project }} \
15+
INFRAKIT_GOOGLE_NAMESPACE_TAGS={{cat "infrakit.scope=" $project | nospace}} \
16+
infrakit plugin start google --log 5 \
1617
> {{env "INFRAKIT_HOME"}}/logs/instance-gcp.log 2>&1 &
1718

1819
echo "Tailing log"

docs/playbooks/intro/gcp/start-plugin.ikt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ echo "Starting GCP plugin"
2020

2121
# Starting docker container for instance plugin
2222
docker run -d --volumes-from infrakit --name instance-gcp \
23-
-v {{$gcpCredentials}} {{$gcpImage}} infrakit-instance-gcp \
24-
--namespace-tags {{cat "infrakit.scope=" $project | nospace}} \
25-
--zone {{ $zone }} --log 5 --project {{ $project }}
23+
-e INFRAKIT_GOOGLE_ZONE={{ $zone }} \
24+
-e INFRAKIT_GOOGLE_PROJECT={{ $project }} \
25+
-e INFRAKIT_GOOGLE_NAMESPACE_TAGS={{cat "infrakit.scope=" $project | nospace}} \
26+
-v {{$gcpCredentials}} {{$gcpImage}} infrakit plugin start google --log 5
2627

2728
{{ end }}

pkg/provider/google/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ $ cat << EOF > gcp-vanilla.json
111111
}
112112
}
113113
}
114+
EOF
114115
```
115116

116117
Finally, instruct the Group plugin to start watching the group:
@@ -119,6 +120,12 @@ Finally, instruct the Group plugin to start watching the group:
119120
$ build/infrakit group-stateless commit gcp-vanilla.json
120121
```
121122

123+
### Permissions
124+
If running on a GCP instance, please make sure that the instance has service accounts enabled:
125+
https://cloud.google.com/compute/docs/access/create-enable-service-accounts-for-instances
126+
127+
You can check that the instance has the correct permissions via: `gcloud compute instances list`
128+
122129
## Group plugin
123130

124131
An InfraKit group plugin which wraps Google Compute Engine's managed instance

pkg/provider/google/plugin/gcloud/api.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"time"
1010

1111
"cloud.google.com/go/compute/metadata"
12-
log "github.com/Sirupsen/logrus"
12+
logutil "github.com/docker/infrakit/pkg/log"
1313
"golang.org/x/oauth2/google"
1414
"google.golang.org/api/compute/v1"
1515
"google.golang.org/api/googleapi"
@@ -112,10 +112,12 @@ type computeServiceWrapper struct {
112112
service *compute.Service
113113
}
114114

115+
var log = logutil.New("module", "provider/google")
116+
115117
// NewAPI creates a new API instance.
116118
func NewAPI(project, zone string) (API, error) {
117119
if project == "" {
118-
log.Debugln("Project not passed on the command line")
120+
log.Debug("Project not passed on the command line", "project", project)
119121

120122
project = findProject()
121123
if project == "" {
@@ -124,16 +126,16 @@ func NewAPI(project, zone string) (API, error) {
124126
}
125127

126128
if zone == "" {
127-
log.Debugln("Zone not passed on the command line")
129+
log.Debug("Zone not passed on the command line")
128130

129131
zone = findZone()
130132
if zone == "" {
131133
return nil, errors.New("Missing zone")
132134
}
133135
}
134136

135-
log.Debugln("Project:", project)
136-
log.Debugln("Zone:", zone)
137+
log.Debug("Project:", "project", project)
138+
log.Debug("Zone:", "zone", zone)
137139

138140
serviceProvider := func() (*compute.Service, error) {
139141
client, err := google.DefaultClient(context.TODO(), compute.ComputeScope)
@@ -159,15 +161,15 @@ func NewAPI(project, zone string) (API, error) {
159161

160162
func findProject() string {
161163
if metadata.OnGCE() {
162-
log.Debugln("- Query the metadata server...")
164+
log.Debug("- Query the metadata server...")
163165

164166
projectID, err := metadata.ProjectID()
165167
if err == nil {
166168
return projectID
167169
}
168170
}
169171

170-
log.Debugln(" - Look for", EnvProject, "env variable...")
172+
log.Debug(" - Look for env var", "project", EnvProject)
171173

172174
value, found := os.LookupEnv(EnvProject)
173175
if found && value != "" {
@@ -179,15 +181,15 @@ func findProject() string {
179181

180182
func findZone() string {
181183
if metadata.OnGCE() {
182-
log.Debugln("- Query the metadata server...")
184+
log.Debug("- Query the metadata server...")
183185

184186
zone, err := metadata.Zone()
185187
if err == nil {
186188
return zone
187189
}
188190
}
189191

190-
log.Debugln(" - Look for", EnvZone, "env variable...")
192+
log.Debug(" - Look for env var", "zone", EnvZone)
191193

192194
value, found := os.LookupEnv(EnvZone)
193195
if found && value != "" {
@@ -290,6 +292,7 @@ func (g *computeServiceWrapper) CreateInstance(name string, settings *InstanceSe
290292
Preemptible: settings.Preemptible,
291293
},
292294
}
295+
log.Debug("Creating instance", "instance", instance)
293296

294297
return g.doCall(g.service.Instances.Insert(g.project, g.zone, instance))
295298
}
@@ -325,26 +328,26 @@ func (g *computeServiceWrapper) attachedDisk(instanceName string, settings DiskS
325328

326329
var existingDisk *compute.Disk
327330
if settings.ReuseExisting {
328-
log.Debugln("Trying to reuse disk", diskName)
331+
log.Debug("Trying to reuse disk", diskName)
329332

330333
disk, err := g.service.Disks.Get(g.project, g.zone, diskName).Do()
331334
if err != nil || disk == nil {
332-
log.Debugln("Couldn't find existing disk", diskName)
335+
log.Debug("Couldn't find existing disk", diskName)
333336
} else if disk.SourceImage != sourceImage {
334-
log.Debugln("Found existing disk that uses a wrong image. Let's delete", diskName)
337+
log.Debug("Found existing disk that uses a wrong image. Let's delete", diskName)
335338
if err := g.doCall(g.service.Disks.Delete(g.project, g.zone, disk.Name)); err != nil {
336339
return nil, err
337340
}
338341
} else {
339-
log.Debugln("Found existing disk", diskName)
342+
log.Debug("Found existing disk", diskName)
340343
existingDisk = disk
341344
}
342345
}
343346

344347
if existingDisk != nil {
345348
disk.Source = existingDisk.SelfLink
346349
} else if settings.Image == "" {
347-
log.Debugln("Creating standalone disk", diskName)
350+
log.Debug("Creating standalone disk", diskName)
348351

349352
if err := g.doCall(g.service.Disks.Insert(g.project, g.zone, &compute.Disk{
350353
Name: diskName,

0 commit comments

Comments
 (0)