Class: OsCtl::Image::Operations::Builder::WaitForNetwork

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

run

Constructor Details

#initialize(builder) ⇒ WaitForNetwork

Returns a new instance of WaitForNetwork.

Parameters:



9
10
11
12
# File 'lib/osctl/image/operations/builder/wait_for_network.rb', line 9

def initialize(builder)
  super()
  @builder = builder
end

Instance Attribute Details

#builderBuilder (readonly)

Returns:



6
7
8
# File 'lib/osctl/image/operations/builder/wait_for_network.rb', line 6

def builder
  @builder
end

Instance Method Details

#executeObject

Raises:



14
15
16
17
18
19
20
21
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
# File 'lib/osctl/image/operations/builder/wait_for_network.rb', line 14

def execute
  script = <<~EOF
    #!/bin/sh

    test_network() {
      curl --head https://images.vpsadminos.org > /dev/null 2>&1
      [ $? = 0 ] && return 0

      wget -q -O - https://images.vpsadminos.org > /dev/null 2>&1
      [ $? = 0 ] && return 0

      ping -c 1 images.vpsadminos.org > /dev/null 2>&1
      [ $? = 0 ] && return 0

      return 1
    }

    test_network
    if [ $? != 0 ] ; then
      echo -n "Waiting for network..."
      for i in $(seq 1 60); do
        test_network && exit 0
        echo -n "."
        sleep 1
      done
      exit 1
    else
      echo "Network online"
    fi
  EOF

  rc = Operations::Builder::ControlledRunscript.run(
    builder,
    script:,
    name: 'osctl-image.wait-for-network'
  )

  return unless rc != 0

  raise OperationError, "network setup failed with exit status #{rc}"
end