This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
start-stop-vpn-droplet
executable file
·134 lines (109 loc) · 2.67 KB
/
start-stop-vpn-droplet
1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/perl
use warnings;
use strict;
use feature qw/say/;
use DigitalOcean;
use File::Slurp qw/read_file/;
use FindBin;
use List::Util qw/all/;
my %conf;
my @signals = qw/INT HUP TERM QUIT/;
foreach(read_file("$FindBin::Bin/config")) {
chomp;
if (m/\A\h*+([^\h]++)\h*+=\h*+([^\h]++)\h*+\z/) {
$conf{$1} = $2
}
}
if (@ARGV) {
if ($ARGV[0] eq 'i' || $ARGV[0] eq '-i' || $ARGV[0] eq 'interactive' || $ARGV[0] eq '--interactive') {
$conf{interactive} = 1
} else {
$conf{interactive} = 0
}
}
my $do = DigitalOcean->new(oauth_token => $conf{oauth_token}, wait_on_actions => 1);
my $vpn_droplet;
my $droplets_collection = $do->droplets;
my $obj;
while($obj = $droplets_collection->next) {
if ($obj->name eq $conf{name}) {
$vpn_droplet = $obj
}
}
# LIST IMAGES/SNAPSHOTS
#my $img = $do->images;
#my $i;
#while($i = $img->next) {
# say $i->name . " : " . $i->id;
#}
# LIST REGIONS
#my $img = $do->regions;
#my $i;
#while($i = $img->next) {
# say $i->name . " : " . $i->slug;
#}
# LIST SIZES
#my $img = $do->sizes;
#my $i;
#while($i = $img->next) {
# say $i->slug . " : " . $i->disk;
#}
sub create_droplet
{
say "Creating Droplet " . $_[1]->{name};
my $d = $_[0]->create_droplet(
name => $_[1]->{name},
size => $_[1]->{size},
image => $_[1]->{image},
region => $_[1]->{region}
);
say "Droplet Id: " . $d->id;
$d = $_[0]->droplet($d->id);
my $ip = $d->networks->v4->[0]->ip_address;
say "Droplet IP: $ip"
if $ip;
if ($ip and exists $_[1]->{nm_id} and $_[1]->{nm_id}) {
my $nm_id = $_[1]->{nm_id};
say "Setting NetworkManager connetion ip address $nm_id to $ip.";
my $nm_settings = `nmcli --fields vpn.data connection show $nm_id`;
$nm_settings =~ s/vpn\.data:\s++//;
$nm_settings =~ s/remote\s*=\s*\K(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/$ip/;
`nmcli connection modify $nm_id vpn.data '$nm_settings'`;
}
$d
}
sub destroy_droplet
{
my $droplet;
if (@_) {
if (all { $_[0] ne $_ } @signals) {
$droplet = $_[0]
} else {
if ($vpn_droplet) {
$droplet = $vpn_droplet
} else {
die "Can't destroy droplet\n"
}
}
}
say "Destroying Droplet " . $droplet->name;
say "Droplet id: " . $droplet->id;
$droplet->delete;
undef
}
unless ($vpn_droplet) {
if ($conf{interactive}) {
say "Interactive mode";
foreach (@signals) {
$SIG{$_} = \&destroy_droplet
}
}
$vpn_droplet = create_droplet $do, \%conf;
if ($conf{interactive}) {
say "Waiting for signal ...";
sleep
}
} else {
destroy_droplet $vpn_droplet;
}
say "Done.";