Class: VagrantLXD::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-lxd/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



27
28
29
# File 'lib/vagrant-lxd/command.rb', line 27

def Command.synopsis
  'manages the LXD provider'
end

Instance Method Details

#attach(args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/vagrant-lxd/command.rb', line 69

def attach(args)
  options = Hash[force: false]

  opts = OptionParser.new do |o|
    o.banner = 'Usage: vagrant lxd attach [-f] [machine ... container]'
    o.separator ''
    o.separator 'Associates a VM with a preexisting LXD container.'
    o.separator ''
    o.separator 'This command can be used to attach an inactive (not created) VM to a'
    o.separator 'preexisting LXD container. Once it has been associated with a container,'
    o.separator 'the machine can be used just like it had been created with `vagrant up`'
    o.separator 'or detached from the container again with `vagrant lxd detach`.'
    o.separator ''
    o.on('-f', '--force', 'Force attachment and ignore missing containers')
  end

  if args.include?('-h') or args.include?('--help')
    @env.ui.info opts.help
    exit 0
  end

  options[:force] ||= args.delete('-f')
  options[:force] ||= args.delete('--force')
  options[:container_name] = args.pop

  with_target_machines(args) do |machine|
    if not container = options[:container_name] || machine.provider_config.name
      machine.ui.warn 'No container name specified, skipping...'
    elsif machine.id == container
      machine.ui.warn "Machine is already attached to container '#{container}', skipping..."
    elsif machine.state.id == Vagrant::MachineState::NOT_CREATED_ID
      machine.ui.info "Attaching to container '#{container}'..."
      begin
        Driver.new(machine).attach(container)
      rescue Driver::ContainerNotFound
        raise unless options[:force]
      end
    elsif options[:force]
      detach([machine.name])
      redo
    else
      machine.ui.error "Machine is already attached to container '#{machine.id}'"
      fail Driver::DuplicateAttachmentFailure, machine_name: machine.name, container: container
    end
  end
end

#detach(args) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/vagrant-lxd/command.rb', line 116

def detach(args)
  options = Hash[force: false]

  opts = OptionParser.new do |o|
    o.banner = 'Usage: vagrant lxd detach [-f] [machine ...]'
    o.separator ''
    o.separator 'Disassociates a VM from its LXD container.'
    o.separator ''
    o.separator 'This command can be used to deactivate a VM without destroying the'
    o.separator 'underlying container. Once detached, the machine can be recreated'
    o.separator 'from scratch with `vagrant up` or associated to a different container'
    o.separator 'by using `vagrant lxd attach`.'
    o.separator ''
    o.on('-f', '--force', 'Force detachment')
  end

  if args.include?('-h') or args.include?('--help')
    @env.ui.info opts.help
    exit 0
  end

  args.delete('-f')
  args.delete('--force')

  with_target_machines(args) do |machine|
    if machine.id.nil? or machine.state.id == Vagrant::MachineState::NOT_CREATED_ID
      machine.ui.warn "Machine is not attached to a container, skipping..."
    else
      machine.ui.info "Detaching from container '#{machine.id}'..."
      Driver.new(machine).detach
    end
  end
end

#exec(args) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/vagrant-lxd/command.rb', line 170

def exec(args)
  opts = OptionParser.new do |o|
    o.banner = 'Usage: vagrant lxd exec [machine ...] -- <command> [args ...]'
    o.separator ''
    o.separator 'Executes a command on the target LXD container(s). Vagrant'
    o.separator 'will wait until each command completes before printing its'
    o.separator 'output and proceeding to the next machine.'
  end

  if args.include?('-h') or args.include?('--help')
    @env.ui.info opts.help
    exit 0
  end

  unless dashes = args.index('--')
    fail Vagrant::Errors::CLIInvalidUsage, help: opts.help
  end

  machines = args.take(dashes)
  command  = args.drop(dashes + 1)
  status   = 0

  with_target_machines(machines) do |machine|
    if machine.id.nil? or machine.state.id != :running
      machine.ui.warn 'Machine is not running, skipping...'
    else
      result = Driver.new(machine).exec(command)
      stdout = result.output[:'1']
      stderr = result.output[:'2']
      status = status unless status.zero?
      machine.ui.info "Running command `#{command.shelljoin}`..."
      machine.ui.detail stdout.chomp unless stdout.empty?
      machine.ui.error  stderr.chomp unless stderr.empty?
    end
  end

  exit status
end

#executeObject



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
# File 'lib/vagrant-lxd/command.rb', line 31

def execute
  main, subcommand, args = split_main_and_subcommand(@argv)

  opts = OptionParser.new do |o|
    o.banner = 'Usage: vagrant lxd <command>'
    o.separator ''
    o.separator 'Commands:'
    o.separator '     attach    associate machine with a running container'
    o.separator '     detach    disassociate machine from a running container'
    o.separator '     exec      execute a command within a running container'
    o.separator '     shadow    print user and group ID mapping information'
    o.separator '     version   print current plugin version'
    o.separator ''
    o.separator 'For help on a specific command, run `vagrant lxd <command> -h`'
  end

  if main.include?('-h') or main.include?('--help')
    @env.ui.info opts.help
    exit 0
  end

  case subcommand
  when 'attach'
    attach(args)
  when 'detach'
    detach(args)
  when 'exec'
    exec(args)
  when 'shadow'
    shadow(args)
  when 'version'
    @env.ui.info 'Vagrant LXD Provider'
    @env.ui.info 'Version ' << Version::VERSION
  else
    fail Vagrant::Errors::CLIInvalidUsage, help: opts.help
  end
end

#shadow(args) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/vagrant-lxd/command.rb', line 209

def shadow(args)
  opts = OptionParser.new do |o|
    o.banner = 'Usage: vagrant lxd shadow --help'
    o.separator ''
    o.separator 'Displays information about user and group ID mapping.'
  end

  if args.include?('-h') or args.include?('--help')
    @env.ui.info I18n.t('vagrant.help.shadow', uid: Process.uid, gid: Process.gid)
  else
    fail Vagrant::Errors::CLIInvalidUsage, help: opts.help
  end
end

#with_target_machines(args) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/vagrant-lxd/command.rb', line 150

def with_target_machines(args)
  machines = args.map(&:to_sym)

  # NOTE We collect all vm names here in order to force Vagrant to
  # load a full local environment, including provider configurations.
  vms = with_target_vms { |_| }.map(&:name)

  # When no machines are given, act on all of them.
  machines = vms if machines.empty?

  # Validate machine names.
  unless vms | machines == vms
    fail Vagrant::Errors::MachineNotFound, name: (machines - vms).first
  end

  machines.each do |name|
    yield @env.machine(name, :lxd)
  end
end