| 1 | # | ||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||
| 3 | # | ||||||||||
| 4 | |||||||||||
| 5 | package Rex::Virtualization::Docker::create; | ||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use v5.14.4; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | our $VERSION = '1.16.1'; # VERSION | |||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | use Rex::Logger; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use Rex::Commands::Gather; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use Rex::Hardware; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use Rex::Commands::Fs; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | ▶ | use Rex::Helper::Run; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use Rex::Commands::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | ▶ | use Rex::File::Parser::Data; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | ▶ | use Rex::Template; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 1 | ▶ | use XML::Simple; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | |||||||||||
| 25 | 0 | ▶ | sub execute { | ||||||||
|
Subroutine
execute: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 26 | 0 | my ( $class, $name, %opt ) = @_; | |||||||||
| 27 | |||||||||||
| 28 | 0 | my $opts = \%opt; | |||||||||
| 29 | 0 | $opts->{name} = $name; | |||||||||
| 30 | |||||||||||
| 31 | 0 | ▶ | unless ($opts) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 0 | die("You have to define the create options!"); | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 0 | ▶ | if ( !exists $opts->{"image"} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 0 | die("You have to set a base image."); | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 0 | ▶ | if ( !exists $opts->{"command"} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 0 | $opts->{command} = ""; | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 0 | my $options = _format_opts($opts); | |||||||||
| 44 | |||||||||||
| 45 | 0 | my @out = i_run "docker run -d $options $opts->{'image'} $opts->{'command'}"; | |||||||||
| 46 | 0 | my $id = pop @out; | |||||||||
| 47 | |||||||||||
| 48 | 0 | return $id; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | sub _format_opts { | ||||||||
|
Subroutine
_format_opts: not called CC=14 SCAR=53.5
| |||||||||||
| 52 | 0 | my ($opts) = @_; | |||||||||
| 53 | |||||||||||
| 54 | # -name="" | ||||||||||
| 55 | # Assign the specified name to the container. If no name is specific docker will generate a random name | ||||||||||
| 56 | 0 | ▶ | if ( !exists $opts->{"name"} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 0 | die("You have to give a name."); | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | # -m="" | ||||||||||
| 61 | # Memory limit (format: <number><optional unit>, where unit = b, k, m or g) | ||||||||||
| 62 | 0 | ▶ | if ( !exists $opts->{"memory"} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | $opts->{"memory"} = '512m'; | |||||||||
| 64 | } | ||||||||||
| 65 | else { | ||||||||||
| 66 | 0 | $opts->{memory} = $opts->{memory}; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | # -c=0 | ||||||||||
| 70 | # CPU shares (relative weight) | ||||||||||
| 71 | 0 | ▶ | if ( !exists $opts->{"cpus"} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 0 | $opts->{"cpus"} = 0; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 0 | my $str = "--name $opts->{'name'} -m $opts->{'memory'} -c $opts->{'cpus'} "; | |||||||||
| 76 | |||||||||||
| 77 | # -e=[] | ||||||||||
| 78 | # Set environment variables | ||||||||||
| 79 | 0 | ▶ | if ( exists $opts->{"env"} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | $str .= '-e ' . join( '-e ', @{ $opts->{'env'} } ) . ' '; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | # -h="" | ||||||||||
| 84 | # Container host name | ||||||||||
| 85 | 0 | ▶ | if ( exists $opts->{"hostname"} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 0 | $str .= "-h $opts->{'hostname'} "; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | # -privileged=false | ||||||||||
| 90 | # Give extended privileges to this container | ||||||||||
| 91 | 0 | ▶ | if ( exists $opts->{"privileged"} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 0 | $str .= "--privileged $opts->{'privileged'} "; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | # -p=[] | ||||||||||
| 96 | # Map a network port to the container | ||||||||||
| 97 | 0 | ▶ | if ( exists $opts->{"forward_port"} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 0 | $str .= '-p ' . join( '-p ', @{ $opts->{'forward_port'} } ) . ' '; | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | # -expose=[] | ||||||||||
| 102 | # Expose a port from the container without publishing it to your host | ||||||||||
| 103 | 0 | ▶ | if ( exists $opts->{"expose_port"} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 0 | $str .= "--expose $opts->{'expose_port'} "; | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | # -dns=[] | ||||||||||
| 108 | # Set custom dns servers for the container | ||||||||||
| 109 | 0 | ▶ | if ( exists $opts->{"dns"} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 0 | $str .= '--dns ' . join( '-dns ', @{ $opts->{'dns'} } ) . ' '; | |||||||||
| 111 | } | ||||||||||
| 112 | |||||||||||
| 113 | # -v=[]: | ||||||||||
| 114 | # Create a bind mount with: [host-dir]:[container-dir]:[rw|ro]. | ||||||||||
| 115 | # If "container-dir" is missing, then docker creates a new volume. | ||||||||||
| 116 | 0 | ▶ | if ( exists $opts->{"share_folder"} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 0 | $str .= '-v ' . join( '-v ', @{ $opts->{'share_folder'} } ) . ' '; | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | # -volumes-from="" | ||||||||||
| 121 | # Mount all volumes from the given container(s) | ||||||||||
| 122 | 0 | ▶ | if ( exists $opts->{"volumes-from"} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | 0 | $str .= "--volumes-from \"$opts->{'volumes-from'}\" "; | |||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | # -lxc-conf=[] | ||||||||||
| 127 | # Add custom lxc options -lxc-conf="lxc.cgroup.cpuset.cpus = 0,1" | ||||||||||
| 128 | 0 | ▶ | if ( exists $opts->{"lxc-conf"} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | 0 | $str .= "--lxc-conf \"$opts->{'lxc-conf'}\" "; | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | # -link="" | ||||||||||
| 133 | # Add link to another container (name:alias) | ||||||||||
| 134 | 0 | ▶ | if ( exists $opts->{"link"} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 0 | $str .= '--link ' . join( '-link ', @{ $opts->{'link'} } ) . ' '; | |||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | 0 | return $str; | |||||||||
| 139 | |||||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | 1 | 1; | |||||||||