Skip to content

Commit 659e47b

Browse files
author
Gerad Suyderhoud
committed
ablity to do all setup for a single team
also a ton of cleanup
1 parent 90d7825 commit 659e47b

14 files changed

+247
-230
lines changed

config/github.coffee

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
request = require 'request'
2+
3+
module.exports = ({ username, password }) ->
4+
throw 'username required' unless username
5+
throw 'password required' unless password
6+
7+
github = (args...) -> github.get(args...)
8+
9+
github.url = (path) ->
10+
auth = "#{username}:#{encodeURIComponent(password)}"
11+
"https://#{auth}@api.github.com/#{path}"
12+
13+
github.request = (method, path, json, next) ->
14+
unless next?
15+
next = json
16+
json = {}
17+
url = github.url(path)
18+
# console.log "github: #{method} #{url}"
19+
request
20+
method: method
21+
url: url
22+
json: json
23+
, next
24+
25+
['get', 'put', 'post'].forEach (method) ->
26+
github[method] = (args...) -> github.request(method, args...)
27+
28+
github

scripts/github-create-repo.coffee

Lines changed: 0 additions & 25 deletions
This file was deleted.

scripts/github-create-teams.coffee

Lines changed: 0 additions & 87 deletions
This file was deleted.

scripts/joyent-create-instances.coffee

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# creates a deploy key on the team's joyent box and adds it to the team's
2+
# github repo
3+
4+
module.exports = setupDeployKey = (options, next) ->
5+
team = options.team
6+
githubAuth = options.githubAuth
7+
8+
# TODO setup deploy key
9+
10+
next()

scripts/setup/setup-dns.coffee

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# creates an A record on linode that points #{slug}.2013.nodeknockout.com to
2+
# the team's ip address
3+
4+
module.exports = setupDNS = (options, next) ->
5+
team = options.team
6+
7+
# TODO setup dns
8+
9+
next()

scripts/setup/setup-github.coffee

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# initialize a github repo for a single team
2+
3+
module.exports = setupGitHub = (options, next) ->
4+
team = options.team
5+
githubAuth = options.githubAuth
6+
7+
github = require('../../config/github')(githubAuth)
8+
spawn = require('child_process').spawn
9+
async = require 'async'
10+
path = require 'path'
11+
12+
rootDir = path.join(__dirname, '..', '..')
13+
14+
async.waterfall [
15+
(next) -> # create repo
16+
console.log team.slug, 'create repo'
17+
github.post 'orgs/nko4/repos',
18+
name: team.slug
19+
homepage: "http://2013.nodeknockout.com/teams/#{team}"
20+
private: true
21+
, next
22+
(res, body, next) -> # create push hook
23+
return next(Error(JSON.stringify(body))) unless body.id
24+
25+
console.log team.slug, 'create hook'
26+
github.post "repos/nko4/#{team.slug}/hooks",
27+
name: 'web'
28+
active: true
29+
config:
30+
url: "http://nodeknockout.com/teams/#{team.code}/commits"
31+
content_type: 'json'
32+
, next
33+
(res, body, next) -> # create team
34+
return next(Error(JSON.stringify(body))) unless body.id
35+
36+
console.log team.slug, 'create team'
37+
github.post 'orgs/nko4/teams',
38+
name: team.name
39+
repo_names: [ "nko4/#{team.slug}" ]
40+
permission: 'admin'
41+
, next
42+
(res, body, next) -> # save team id
43+
return next(Error(JSON.stringify(body))) unless body.id
44+
45+
console.log team.slug, 'save github info'
46+
team.github = body
47+
team.save next
48+
(team, n, next) -> # get people
49+
console.log team.slug, 'get people'
50+
team.people (err, people) ->
51+
next err, team, people
52+
(team, people, next) -> # add members
53+
async.forEach people, (person, next) ->
54+
console.log team.slug, 'add people', person.github.login
55+
github.put "teams/#{team.github.id}/members/#{person.github.login}", next
56+
, next
57+
(next) -> # seed repo
58+
console.log team.slug, 'seed repo'
59+
createRepo = spawn path.join(__dirname, './setup-repo.sh'),
60+
[ team.slug, team.code, team.name, team.github.id ],
61+
cwd: rootDir
62+
createRepo.stdout.on 'data', (s) -> console.log s.toString()
63+
createRepo.stderr.on 'data', (s) -> console.log s.toString()
64+
createRepo.on 'error', next
65+
createRepo.on 'exit', -> next()
66+
], next

scripts/setup/setup-joyent.coffee

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# creates a joyent instance for a single team
2+
3+
module.exports = setupJoyent = (options, next) ->
4+
team = options.team
5+
image = options.joyent?.image ? 'd2ba0f30-bbe8-11e2-a9a2-6bc116856d85' # ubuntu 12.04 - 64bit v2.4.2
6+
packg = options.joyent?.package ? 'ec521e7a-8799-4ffc-a914-bb41233f25f5' # 512mb ram - kvm
7+
8+
async = require 'async'
9+
joyent = require '../../config/joyent'
10+
11+
# joyent.listImages (err, res) -> console.dir(res)
12+
# joyent.listPackages (err, res) -> console.dir(res)
13+
14+
createMachine = (next) ->
15+
console.log team.slug, 'create machine'
16+
joyent.createMachine
17+
name: team.slug
18+
image: image
19+
package: packg
20+
, next
21+
22+
waitUntilRunning = (machine, next) ->
23+
console.log team.slug, 'wait until running'
24+
25+
secs = 15
26+
i = 0
27+
28+
do check = ->
29+
joyent.getMachine machine, (err, res) ->
30+
return next(err) if err
31+
switch res.state
32+
when 'provisioning'
33+
console.log team.slug, "#{res.state} (#{i * secs}s)"
34+
setTimeout check, secs * 1000
35+
i += 1
36+
when 'running'
37+
console.log team.slug, res.state
38+
return next(null, res)
39+
else
40+
return next("Error: #{machine.name} in unexpected state: '#{res.state}'")
41+
42+
saveMachine = (machine, next) ->
43+
console.log team.slug, 'save machine ip'
44+
team.ip = machine.ips[0]
45+
team.machine = machine
46+
team.save next
47+
48+
async.waterfall [createMachine, waitUntilRunning, saveMachine], next
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh
2-
set -evu
2+
set -eu
33

44
slug=$1
55
code=$2
@@ -33,17 +33,17 @@ ssh root@${slug}.2013.nodeknockout.com
3333
We've already set up a basic node server for you. Details:
3434
3535
* Ubuntu 12.10 (Precise) - 64-bit
36-
* `/home/deploy/current/server.js` - server
37-
* `/home/deploy/shared/logs/server.log` - server logs
38-
* `runit` keeps the server running.
39-
* `sv restart serverjs` - restarts
40-
* `sv start serverjs` - starts
41-
* `sv stop serverjs` - stops
42-
* `runsvdir -P /etc/service log` - to see logs
43-
* `cat /etc/service/serverjs/run` - to see the config
44-
45-
You can use the `./deploy` script included in this repo to deploy to it right
46-
now. Advanced users, feel free to tweak.
36+
* server.js is at: \`/home/deploy/current/server.js\`
37+
* logs are at: \`/home/deploy/shared/logs/server.log\`
38+
* \`runit\` keeps the server running.
39+
* \`sv restart serverjs\` - restarts
40+
* \`sv start serverjs\` - starts
41+
* \`sv stop serverjs\` - stops
42+
* \`runsvdir -P /etc/service log\` - to see logs
43+
* \`cat /etc/service/serverjs/run\` - to see the config
44+
45+
You can use the \`./deploy\` script included in this repo to deploy to your
46+
server right now. Advanced users, feel free to tweak.
4747
4848
[Find out more](http://blog.nodeknockout.com/deploying-to-joyent)
4949
@@ -146,4 +146,4 @@ EOF
146146
git add .
147147
git commit -m Instructions
148148
git remote add origin [email protected]:nko4/${slug}.git
149-
git push -f -u origin master
149+
git push origin master

0 commit comments

Comments
 (0)