Skip to content

Commit 1bfb106

Browse files
committed
cmd/scaleway, env/linux-arm/scaleway: notes, configs, and tools for Scaleway
This is what I used to start the ARM scaleway servers. I forgot to mail it out. I'd like to submit this before I tweak it further. Updates golang/go#8647 Change-Id: Icf789de4e3acae8084fd75151ef5ef02d8073b73 Reviewed-on: https://go-review.googlesource.com/10052 Reviewed-by: Andrew Gerrand <[email protected]>
1 parent eb52e71 commit 1bfb106

File tree

4 files changed

+254
-0
lines changed

4 files changed

+254
-0
lines changed

cmd/scaleway/scaleway.go

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// Copyright 2015 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// The scaleway command creates ARM servers on Scaleway.com.
6+
package main
7+
8+
import (
9+
"bytes"
10+
"encoding/json"
11+
"flag"
12+
"fmt"
13+
"io/ioutil"
14+
"log"
15+
"net/http"
16+
"os"
17+
"path/filepath"
18+
"strings"
19+
)
20+
21+
var (
22+
token = flag.String("token", "", "API token")
23+
org = flag.String("org", "1f34701d-668b-441b-bf08-0b13544e99de", "Organization ID (default is [email protected]'s account)")
24+
image = flag.String("image", "b9fcca88-fa85-4606-a2b2-3c8a7ff94fbd", "Disk image ID; default is the snapshot we made last")
25+
num = flag.Int("n", 20, "Number of servers to create")
26+
)
27+
28+
func main() {
29+
flag.Parse()
30+
if *token == "" {
31+
file := filepath.Join(os.Getenv("HOME"), "keys/go-scaleway.token")
32+
slurp, err := ioutil.ReadFile(file)
33+
if err != nil {
34+
log.Fatalf("No --token specified and error reading backup token file: %v", err)
35+
}
36+
*token = strings.TrimSpace(string(slurp))
37+
}
38+
39+
cl := &Client{Token: *token}
40+
serverList, err := cl.Servers()
41+
if err != nil {
42+
log.Fatal(err)
43+
}
44+
servers := map[string]*Server{}
45+
for _, s := range serverList {
46+
servers[s.Name] = s
47+
}
48+
49+
for i := 1; i <= *num; i++ {
50+
name := fmt.Sprintf("go-build-%d", i)
51+
_, ok := servers[name]
52+
if !ok {
53+
body, err := json.Marshal(createServerRequest{
54+
Org: *org,
55+
Name: name,
56+
Image: *image,
57+
})
58+
if err != nil {
59+
log.Fatal(err)
60+
}
61+
log.Printf("Doing req %q for token %q", body, *token)
62+
req, err := http.NewRequest("POST", "https://api.scaleway.com/servers", bytes.NewReader(body))
63+
if err != nil {
64+
log.Fatal(err)
65+
}
66+
req.Header.Set("Content-Type", "application/json")
67+
req.Header.Set("X-Auth-Token", *token)
68+
res, err := http.DefaultClient.Do(req)
69+
if err != nil {
70+
log.Fatal(err)
71+
}
72+
log.Printf("Create of %v: %v", i, res.Status)
73+
res.Body.Close()
74+
}
75+
}
76+
77+
serverList, err = cl.Servers()
78+
if err != nil {
79+
log.Fatal(err)
80+
}
81+
for _, s := range serverList {
82+
if s.State == "stopped" {
83+
log.Printf("Powering on %s = %v", s.ID, cl.PowerOn(s.ID))
84+
}
85+
}
86+
}
87+
88+
type createServerRequest struct {
89+
Org string `json:"organization"`
90+
Name string `json:"name"`
91+
Image string `json:"image"`
92+
}
93+
94+
type Client struct {
95+
Token string
96+
}
97+
98+
func (c *Client) PowerOn(serverID string) error {
99+
return c.serverAction(serverID, "poweron")
100+
}
101+
102+
func (c *Client) serverAction(serverID, action string) error {
103+
req, _ := http.NewRequest("POST", "https://api.scaleway.com/servers/"+serverID+"/action", strings.NewReader(fmt.Sprintf(`{"action":"%s"}`, action)))
104+
req.Header.Set("Content-Type", "application/json")
105+
req.Header.Set("X-Auth-Token", c.Token)
106+
res, err := http.DefaultClient.Do(req)
107+
if err != nil {
108+
return err
109+
}
110+
defer res.Body.Close()
111+
if res.StatusCode/100 != 2 {
112+
return fmt.Errorf("Error doing %q on %s: %v", action, serverID, res.Status)
113+
}
114+
return nil
115+
}
116+
117+
func (c *Client) Servers() ([]*Server, error) {
118+
req, _ := http.NewRequest("GET", "https://api.scaleway.com/servers", nil)
119+
req.Header.Set("X-Auth-Token", c.Token)
120+
res, err := http.DefaultClient.Do(req)
121+
if err != nil {
122+
return nil, err
123+
}
124+
defer res.Body.Close()
125+
if res.StatusCode != 200 {
126+
return nil, fmt.Errorf("Failed to get Server list: %v", res.Status)
127+
}
128+
var jres struct {
129+
Servers []*Server `json:"servers"`
130+
}
131+
err = json.NewDecoder(res.Body).Decode(&jres)
132+
return jres.Servers, err
133+
}
134+
135+
type Server struct {
136+
ID string `json:"id"`
137+
Name string `json:"name"`
138+
PublicIP *IP `json:"public_ip"`
139+
PrivateIP string `json:"private_ip"`
140+
Tags []string `json:"tags"`
141+
State string `json:"state"`
142+
Image *Image `json:"image"`
143+
}
144+
145+
type Image struct {
146+
ID string `json:"id"`
147+
Name string `json:"name"`
148+
}
149+
150+
type IP struct {
151+
ID string `json:"id"`
152+
Address string `json:"address"`
153+
}

env/linux-arm/scaleway/Dockerfile

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2015 The Go Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style
3+
# license that can be found in the LICENSE file.
4+
5+
FROM armbuild/ubuntu:trusty
6+
7+
MAINTAINER golang-dev <[email protected]>
8+
ENV DEBIAN_FRONTEND noninteractive
9+
10+
RUN apt-get update
11+
RUN apt-get install -y --no-install-recommends curl
12+
13+
RUN echo "607573c55dc89d135c3c9c84bba6ba6095a37a1e go.tar.gz" > /tmp/go.tar.gz.sha1
14+
RUN cd /tmp && \
15+
curl --silent -o go.tar.gz http://dave.cheney.net/paste/go1.4.2.linux-arm~multiarch-armv7-1.tar.gz && \
16+
sha1sum -c go.tar.gz.sha1 && \
17+
tar -C /usr/local -zxvf go.tar.gz && \
18+
rm -rf go.tar.gz
19+
20+
RUN apt-get install -y --no-install-recommends ca-certificates
21+
22+
RUN mkdir /usr/local/gomaster
23+
ENV GO_MASTER_VERSION d4bb72b4
24+
RUN curl https://go.googlesource.com/go/+archive/$GO_MASTER_VERSION.tar.gz | tar -C /usr/local/gomaster -zxv
25+
ENV GOROOT /usr/local/gomaster
26+
RUN echo "devel $GO_MASTER_VERSION" > $GOROOT/VERSION
27+
28+
RUN apt-get install -y --no-install-recommends gcc
29+
RUN apt-get install -y --no-install-recommends libc6-dev
30+
31+
ENV GOROOT_BOOTSTRAP /usr/local/go
32+
RUN cd $GOROOT/src && ./make.bash
33+
34+
RUN apt-get install -y --no-install-recommends git-core
35+
36+
ENV GOPATH /gopath
37+
RUN mkdir /gopath
38+
RUN $GOROOT/bin/go get golang.org/x/build/cmd/buildlet
39+
40+
ADD run-buildlet.sh /usr/local/bin/run-buildlet.sh
41+
42+
# Environment variables to be passed to "docker run"
43+
# for linux-arm and linux-arm-arm5:
44+
ENV BUILDKEY_ARM="" BUILDKEY_ARM5=""
45+
46+
ENTRYPOINT ["/usr/local/bin/run-buildlet.sh"]

env/linux-arm/scaleway/README

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
To create a fresh builder host: (should only be needed once)
2+
3+
Create a new Scaleway server with type "Docker 1.5" (second tab).
4+
5+
Make a tmpfs:
6+
# echo "tmpfs /tmpfs tmpfs" >> /etc/fstab
7+
# mkdir /tmpfs
8+
9+
Make a 2GB swap file:
10+
# dd if=/dev/zero of=/swap count=2097152 obs=1024
11+
# mkswap /swap
12+
# chmod 0600 /swap
13+
# echo "/swap none swap loop 0 0" >> /etc/fstab
14+
15+
Reboot.
16+
17+
Should see swaps in "cat /proc/swaps" and tmpfs in "cat /proc/mounts" now.
18+
19+
* Copy the contents of this directory to the server.
20+
21+
* Go into that directory and:
22+
# docker build -t buildlet .
23+
24+
Add to /etc/rc.local:
25+
26+
(mkdir -p /tmpfs/buildertmp && docker run -e BUILDKEY_ARM=xxx -e BUILDKEY_ARM5=xxx -v=/tmpfs/buildertmp:/tmp --restart=always -d --name=buildlet buildlet) &
27+
sleep 5
28+
29+
.. before the exit 0
30+
31+
Power it down (with ARCHIVE),
32+
33+
Snapshot the disk.
34+
35+
Start a bunch of them. (see golang.org/x/build/cmd/scaleway)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
# Meant to be run under Docker.
4+
5+
if [ "$BUILDKEY_ARM" == "" ]; then
6+
env
7+
echo "ERROR: BUILDKEY_ARM not set. (using docker run?)" >&2
8+
exit 1
9+
fi
10+
if [ "$BUILDKEY_ARM5" == "" ]; then
11+
env
12+
echo "ERROR: BUILDKEY_ARM5 not set. (using docker run?)" >&2
13+
exit 1
14+
fi
15+
16+
17+
set -e
18+
echo $BUILDKEY_ARM > /root/.gobuildkey-linux-arm
19+
echo $BUILDKEY_ARM5 > /root/.gobuildkey-linux-arm-arm5
20+
exec /gopath/bin/buildlet -reverse=linux-arm,linux-arm-arm5 -coordinator farmer.golang.org:443

0 commit comments

Comments
 (0)