2
2
# -*- coding: utf-8 -*-
3
3
4
4
require 'fileutils'
5
-
6
- require './coreos_config.rb '
5
+ require 'open-uri'
6
+ require 'yaml '
7
7
8
8
Vagrant . require_version '>= 1.6.0'
9
9
10
10
CLOUD_CONFIG_PATH = File . join ( File . dirname ( __FILE__ ) , 'user-data' )
11
11
12
+ update_channel = 'alpha'
13
+ image_version = 'current'
14
+ vm_memory = 512
15
+ vm_cpus = 1
16
+
17
+ cluster = [
18
+ { name : 'core-01' , hostname : 'core-01' , ip : '172.17.8.101' , metadata : 'role=nginx' } ,
19
+ { name : 'core-02' , hostname : 'core-02' , ip : '172.17.8.102' , metadata : 'role=appside' } ,
20
+ { name : 'core-03' , hostname : 'core-03' , ip : '172.17.8.103' , metadata : 'role=devside' }
21
+ ]
22
+
23
+ # Used to fetch a new discovery token for a cluster of size 3
24
+ new_discovery_url = "https://discovery.etcd.io/new?size=#{ cluster . size } "
25
+ token = open ( new_discovery_url ) . read
26
+
12
27
Vagrant . configure ( '2' ) do |config |
13
28
# always use Vagrants insecure key
14
29
config . ssh . insert_key = false
15
30
16
- config . vm . box = "coreos-%s" % $update_channel
17
- if $image_version != 'current'
18
- config . vm . box_version = $image_version
19
- end
20
- config . vm . box_url = "http://%s.release.core-os.net/amd64-usr/%s/coreos_production_vagrant.json" % [ $update_channel, $image_version]
31
+ config . vm . box = "coreos-%s" % update_channel
32
+ config . vm . box_url = "http://%s.release.core-os.net/amd64-usr/%s/coreos_production_vagrant.json" % [ update_channel , image_version ]
21
33
22
34
config . vm . provider :virtualbox do |v |
23
35
# On VirtualBox, we don't have guest additions or a functional vboxsf
@@ -26,33 +38,34 @@ Vagrant.configure('2') do |config|
26
38
v . functional_vboxsf = false
27
39
end
28
40
29
- ( 1 ..$num_instances) . each do |i |
30
- config . vm . define vm_name = "%s-%02d" % [ $instance_name_prefix, i ] do |config |
31
- config . vm . hostname = vm_name
32
-
33
- if $enable_serial_logging
34
- logdir = File . join ( File . dirname ( __FILE__ ) , 'log' )
35
- FileUtils . mkdir_p ( logdir )
36
-
37
- serialFile = File . join ( logdir , "%s-serial.txt" % vm_name )
38
- FileUtils . touch ( serialFile )
39
- end
40
-
41
- $forwarded_ports. each do |guest , host |
42
- config . vm . network 'forwarded_port' , guest : guest , host : host , auto_correct : true
43
- end
41
+ cluster . each do |node |
42
+ config . vm . define node [ :name ] do |config |
43
+ config . vm . hostname = node [ :hostname ]
44
44
45
45
config . vm . provider :virtualbox do |v |
46
- v . name = vm_name
47
- v . memory = $ vm_memory
48
- v . cpus = $ vm_cpus
46
+ v . name = node [ :name ]
47
+ v . memory = vm_memory
48
+ v . cpus = vm_cpus
49
49
end
50
50
51
- ip = "172.17.8.#{ i +100 } "
52
- config . vm . network :private_network , ip : ip
51
+ config . vm . network :private_network , ip : node [ :ip ]
53
52
54
53
# cloudconfig-init
55
- config . vm . provision :file , :source => "#{ CLOUD_CONFIG_PATH } " , :destination => '/tmp/vagrantfile-user-data'
54
+ data = YAML . load ( IO . readlines ( 'user-data' ) [ 1 ..-1 ] . join )
55
+
56
+ # Add discovery url
57
+ data [ 'coreos' ] [ 'etcd2' ] [ 'discovery' ] = token
58
+
59
+ # Add metadata
60
+ data [ 'coreos' ] [ 'fleet' ] [ 'metadata' ] = node [ :metadata ]
61
+
62
+ yaml = YAML . dump ( data )
63
+
64
+ source_file = File . join ( File . dirname ( __FILE__ ) , "user-data_#{ node [ :name ] } " )
65
+ FileUtils . rm source_file if File . exists? ( source_file )
66
+ File . open ( source_file , 'w' ) { |file | file . write ( "#cloud-config\n \n #{ yaml } " ) }
67
+
68
+ config . vm . provision :file , :source => source_file , :destination => '/tmp/vagrantfile-user-data'
56
69
config . vm . provision :shell , :inline => 'mv /tmp/vagrantfile-user-data /var/lib/coreos-vagrant/' , :privileged => true
57
70
end
58
71
end
0 commit comments