Class: OsCtl::Image::Operations::Builder::Create

Inherits:
OsCtl::Image::Operations::Base show all
Defined in:
lib/osctl/image/operations/builder/create.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from OsCtl::Image::Operations::Base

run

Constructor Details

#initialize(builder, base_dir) ⇒ Create

Returns a new instance of Create.



16
17
18
19
20
# File 'lib/osctl/image/operations/builder/create.rb', line 16

def initialize(builder, base_dir)
  @builder = builder
  @base_dir = base_dir
  @setup_id = SecureRandom.hex(4)
end

Instance Attribute Details

#base_dirString (readonly)

Returns:

  • (String)


11
12
13
# File 'lib/osctl/image/operations/builder/create.rb', line 11

def base_dir
  @base_dir
end

#builderBuilder (readonly)

Returns:



8
9
10
# File 'lib/osctl/image/operations/builder/create.rb', line 8

def builder
  @builder
end

#setup_idString (readonly)

Returns:

  • (String)


14
15
16
# File 'lib/osctl/image/operations/builder/create.rb', line 14

def setup_id
  @setup_id
end

Instance Method Details

#builder_base_dirObject (protected)



88
89
90
# File 'lib/osctl/image/operations/builder/create.rb', line 88

def builder_base_dir
  "/build/basedir.#{setup_id}"
end

#executeObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/osctl/image/operations/builder/create.rb', line 22

def execute
  client = OsCtldClient.new
  client.batch do
    begin
      client.create_container_from_repo(
        builder.ctid,
        builder.distribution,
        builder.version,
        builder.arch,
        builder.vendor,
        builder.variant,
      )

      # When the command above returns, the CT is still in an unknown state,
      # so network interfaces cannot be added... this should be fixed
      # in osctld, so that it returns after the ct is in a correct state
      sleep(3)

      client.set_container_attr(
        builder.ctid,
        'org.vpsadminos.osctl-image:type',
        'builder'
      )

      client.add_netif_bridge(builder.ctid, 'eth0', 'lxcbr0')

      client.start_container(builder.ctid)

      # Give the container some time to start
      sleep(5)

      builder.load_attrs(client)

      client.bind_mount(builder.ctid, base_dir, builder_base_dir)
      client.activate_mount(builder.ctid, builder_base_dir)

      Operations::Builder::WaitForNetwork.run(builder)

      rc = client.exec(builder.ctid, [
        File.join(builder_base_dir, 'bin', 'runner'),
        'builder',
        'setup',
        builder.name,
      ])

      client.unmount(builder.ctid, builder_base_dir)

      if rc != 0
        raise OperationError,
              "builder setup failed with exit status #{rc}"
      end

    rescue OsCtl::Client::Error,
           OperationError => e
      puts "* error occurred: #{e.message}, cleaning up"

      if client.find_container(builder.ctid)
        client.delete_container(builder.ctid)
      end

      raise e
    end
  end
end