fig2coreos 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/fig2coreos +41 -0
  3. data/lib/fig2coreos.rb +187 -0
  4. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 56cc01e8482595c4315fffdf300e87d9b34238f1
4
+ data.tar.gz: 9c9bf73108bdfe6a4808830baa02a9d42c9de7bb
5
+ SHA512:
6
+ metadata.gz: f41ae4f0ded5d19a6420e08f68034b51771db0bff807a6983ad7cf2fcfe253e2e151b957bba671136c3576d3067a9296cdf6dc10c5fd170413e8b3d4ea27ec3e
7
+ data.tar.gz: 5a94d05010ebc4deb760b3e8ab3526f6923482488345bac6d24b4075d7b085ebfe68d7c7172ebb918a60207b42e45f9aca34e4193919ed6aef12324311926815
data/bin/fig2coreos ADDED
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "lib", "fig2coreos"))
4
+
5
+ FIG2COREOS_BANNER = "Usage: fig2coreos [options] APP_NAME FIG_YAML OUTPUT_DIRECTORY"
6
+
7
+ options = {type: "vagrant"}
8
+ OptionParser.new do |opts|
9
+ opts.banner = FIG2COREOS_BANNER
10
+
11
+ opts.on("-t", "--type TYPE", "Output type: vagrant (default), systemd (just the systemd service files)") do |type|
12
+ options[:type] = type
13
+ end
14
+ end.parse!
15
+
16
+ if ARGV[0].nil?
17
+ puts "[ERROR] APP_NAME required"
18
+ puts FIG2COREOS_BANNER
19
+ exit -1
20
+ end
21
+
22
+ if !File.file?(File.expand_path(ARGV[1].to_s))
23
+ puts "[ERROR] FIG_YAML path required"
24
+ puts FIG2COREOS_BANNER
25
+ exit -1
26
+ end
27
+
28
+ if !ARGV[2] || !File.directory?(File.expand_path(ARGV[2].to_s))
29
+ puts "[ERROR] OUTPUT_DIRECTORY required"
30
+ puts FIG2COREOS_BANNER
31
+ exit -1
32
+ end
33
+
34
+ vagrant_version = `vagrant --version`.split.last
35
+
36
+ if options[:type] == "vagrant" && Gem::Version.new(vagrant_version) < Gem::Version.new("1.4.3")
37
+ puts "[ERROR] Your version of vagrant (#{vagrant_version}) is out of date, please upgrade to at least 1.4.3 at http://www.vagrantup.com/downloads.html or change the --type"
38
+ exit -1
39
+ end
40
+
41
+ puts Fig2CoreOS.convert(ARGV[0], ARGV[1], ARGV[2], options)
data/lib/fig2coreos.rb ADDED
@@ -0,0 +1,187 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'yaml'
5
+ require 'fileutils'
6
+
7
+ class Fig2CoreOS
8
+ def self.convert(app_name, fig_file, output_dir, options={})
9
+ Fig2CoreOS.new(app_name, fig_file, output_dir, options)
10
+ end
11
+
12
+ def initialize(app_name, fig_file, output_dir, options={})
13
+ @app_name = app_name
14
+ @fig = YAML.load_file(fig_file.to_s)
15
+ @output_dir = File.expand_path(output_dir.to_s)
16
+
17
+ # clean and setup directory structure
18
+ FileUtils.rm_rf(File.join(@output_dir, "media"))
19
+ FileUtils.rm_rf(File.join(@output_dir, "setup-coreos.sh"))
20
+ FileUtils.rm_rf(File.join(@output_dir, "Vagrantfile"))
21
+ FileUtils.mkdir_p(File.join(@output_dir, "media", "state", "units"))
22
+
23
+ create_service_files
24
+
25
+ create_vagrant_file if options[:type] == "vagrant"
26
+
27
+ exit 0
28
+ end
29
+
30
+ def create_service_files
31
+ @fig.each do |service_name, service|
32
+ image = service["image"]
33
+ ports = (service["ports"] || []).map{|port| "-p #{port}"}
34
+ volumes = (service["volumes"] || []).map{|volume| "-v #{volume}"}
35
+ links = (service["links"] || []).map{|link| "--link #{link}_1:#{link}_1"}
36
+ envs = (service["environment"] || []).map do |env_name, env_value|
37
+ "-e \"#{env_name}=#{env_value}\""
38
+ end
39
+
40
+ after = if service["links"]
41
+ "#{service["links"].last}.1"
42
+ else
43
+ "docker"
44
+ end
45
+
46
+ File.open(File.join(@output_dir, "media", "state", "units", "#{service_name}.1.service"), "w") do |file|
47
+ file << <<-eof
48
+ [Unit]
49
+ Description=Run #{service_name}_1
50
+ After=#{after}.service
51
+ Requires=#{after}.service
52
+
53
+ [Service]
54
+ Restart=always
55
+ RestartSec=10s
56
+ ExecStartPre=/usr/bin/docker ps -a -q | xargs docker rm
57
+ ExecStart=/usr/bin/docker run -rm -name #{service_name}_1 #{volumes.join(" ")} #{links.join(" ")} #{envs.join(" ")} #{ports.join(" ")} #{image}
58
+ ExecStartPost=/usr/bin/docker ps -a -q | xargs docker rm
59
+ ExecStop=/usr/bin/docker kill #{service_name}_1
60
+ ExecStopPost=/usr/bin/docker ps -a -q | xargs docker rm
61
+
62
+ [Install]
63
+ WantedBy=local.target
64
+ eof
65
+ end
66
+
67
+ File.open(File.join(@output_dir, "media", "state", "units", "#{service_name}-discovery.1.service"), "w") do |file|
68
+ file << <<-eof
69
+ [Unit]
70
+ Description=Announce #{service_name}_1
71
+ BindsTo=#{service_name}.1.service
72
+
73
+ [Service]
74
+ ExecStart=/bin/sh -c "while true; do etcdctl set /services/#{service_name}/#{service_name}_1 '{ \\"host\\": \\"%H\\", \\"port\\": #{service["ports"].first.to_s.split(":").first}, \\"version\\": \\"52c7248a14\\" }' --ttl 60;sleep 45;done"
75
+ ExecStop=/usr/bin/etcdctl rm /services/#{service_name}/#{service_name}_1
76
+
77
+ [X-Fleet]
78
+ X-ConditionMachineOf=#{service_name}.1.service
79
+ eof
80
+ end
81
+ end
82
+ end
83
+
84
+ def create_vagrant_file
85
+ File.open(File.join(@output_dir, "setup-coreos.sh"), "w") do |file|
86
+ file << <<-eof
87
+ # Switch to root for setting up systemd
88
+ sudo -i
89
+
90
+ # Clear old containers
91
+ systemctl stop local-enable.service
92
+ systemctl stop etcd-cluster.service
93
+ /usr/bin/docker ps -a -q | xargs docker kill
94
+ /usr/bin/docker ps -a -q | xargs docker rm
95
+
96
+ # Copy the services into place
97
+ eof
98
+ @fig.each do |service_name, service|
99
+ file << "cp " + File.join("share", "media", "state", "units", "#{service_name}.1.service") + " /media/state/units/#{service_name}.1.service\n"
100
+ file << "cp " + File.join("share", "media", "state", "units", "#{service_name}-discovery.1.service") + " /media/state/units/#{service_name}-discovery.1.service\n\n"
101
+ end
102
+
103
+ file << <<-eof
104
+
105
+ # Fix etcd-cluster setup
106
+ HOSTNAME=$(</proc/sys/kernel/hostname)
107
+
108
+ if [ ! -d /var/run/etcd/${HOSTNAME} ]
109
+ then
110
+ mkdir /var/run/etcd/${HOSTNAME}
111
+ chown core:core /var/run/etcd/${HOSTNAME}
112
+ fi
113
+
114
+ echo 192.168.10.2 > /var/run/etcd/MY_IP
115
+
116
+ # Replace the existing line:
117
+ sed -i -e "s@ExecStart=.*@ExecStart=/usr/bin/etcd -s 192.168.10.2:7001 -sl 0.0.0.0 -cl 0.0.0.0 -c 192.168.10.2:4001 ${CLUSTER_OPT} -d /var/run/etcd/${HOSTNAME} -n $HOSTNAME@" /media/state/units/etcd-cluster.service
118
+
119
+ # Start containers and fleet
120
+ systemctl daemon-reload
121
+ systemctl start etcd-cluster.service
122
+ systemctl start local-enable.service
123
+ systemctl start fleet
124
+
125
+ etcdctl mkdir /services
126
+ eof
127
+ @fig.each do |service_name, service|
128
+ file << "etcdctl mkdir /services/#{service_name}\n"
129
+ end
130
+ file << "cd /media/state/units; for service in *.service; do fleetctl start $service; done\n"
131
+ file << "sleep 3; /usr/bin/docker ps -a -q | xargs docker rm\n"
132
+ file << "sleep 3; /usr/bin/docker ps -a -q | xargs docker rm\n"
133
+ file << "echo 'SUCCESS'\n"
134
+ file << "exit 0\n"
135
+
136
+ file.chmod(0755)
137
+ end
138
+
139
+ File.open(File.join(@output_dir, "Vagrantfile"), "w") do |file|
140
+ file << <<-eof
141
+ # -*- mode: ruby -*-
142
+ # vi: set ft=ruby :
143
+
144
+ $expose_port = 8080
145
+
146
+ Vagrant.configure("2") do |config|
147
+ config.vm.box = "coreos"
148
+ config.vm.box_url = "http://storage.core-os.net/coreos/amd64-generic/dev-channel/coreos_production_vagrant.box"
149
+ config.vm.hostname = "coreos-#{@app_name}"
150
+
151
+ config.vm.provider :virtualbox do |vb, override|
152
+ vb.name = "vagrant-coreos-docker-converted-from-fig-#{@app_name}"
153
+ # Fix docker not being able to resolve private registry in VirtualBox
154
+ vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
155
+ vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
156
+ end
157
+
158
+ # Share this folder so provisioner can access Dockerfile an apache.service
159
+ # This is from coreos/coreos-vagrant, but won't work on Windows hosts
160
+ config.vm.network "private_network", ip: "192.168.10.2"
161
+ config.vm.synced_folder ".", "/home/core/share", id: "core", :nfs => true, :mount_options => ['nolock,vers=3,udp']
162
+
163
+ # Forward port 80 from coreos (which is forwarding port 80 of the container)
164
+ config.vm.network :forwarded_port, guest: 80, host: $expose_port, host_ip: "127.0.0.1"
165
+
166
+ config.vm.provider :vmware_fusion do |vb, override|
167
+ override.vm.box_url = "http://storage.core-os.net/coreos/amd64-generic/dev-channel/coreos_production_vagrant_vmware_fusion.box"
168
+ end
169
+
170
+ # plugin conflict
171
+ if Vagrant.has_plugin?("vagrant-vbguest") then
172
+ config.vbguest.auto_update = false
173
+ end
174
+
175
+ # Provision
176
+ config.vm.provision "shell", :path => "setup-coreos.sh"
177
+ end
178
+ eof
179
+ end
180
+
181
+ if File.directory?(File.join(@output_dir, ".vagrant"))
182
+ puts "[SUCCESS] Try this: cd #{@output_dir} && vagrant reload --provision"
183
+ else
184
+ puts "[SUCCESS] Try this: cd #{@output_dir} && vagrant up"
185
+ end
186
+ end
187
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fig2coreos
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lucas Carlson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Convert fig to coreos formatted configuration files
14
+ email: lucas@rufy.com
15
+ executables:
16
+ - fig2coreos
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/fig2coreos.rb
21
+ - bin/fig2coreos
22
+ homepage: http://github.com/CenturyLinkLabs/fig2coreos
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.0.3
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Convert fig to coreos formatted configuration files
46
+ test_files: []