-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbimivalidator-helper
executable file
·95 lines (75 loc) · 2.52 KB
/
bimivalidator-helper
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
#!/usr/bin/env perl
my $argv = $ARGV[0];
sub install_docker_image {
print "Installing the docker image\n\n";
system(
'docker', 'run',
'--rm',
'--user', 'nobody',
'--entrypoint', 'mailbimi',
'marcbradshaw/bimivalidator:latest', '--version',
);
}
sub remove_docker_image {
print "Removing the old docker image\n\n";
system('docker', 'image', 'rm', 'marcbradshaw/bimivalidator:latest');
}
sub install_systemd_service {
install_docker_image();
my $payload = '[Unit]
Description=BIMI Validator service
After=network.target
[Service]
Type=simple
ExecStart=docker run --name bimivalidator --rm -p 5000:5000 --user nobody marcbradshaw/bimivalidator:latest
Restart=on-failure
[Install]
WantedBy=multi-user.target';
print "Installing and enabling the systemd service\n\n";
open my $outf, '>', '/etc/systemd/system/bimivalidator.service' || die 'could not write service file';
print $outf $payload;
close $outf;
system('systemctl', 'daemon-reload');
system('systemctl', 'enable', 'bimivalidator.service');
system('systemctl', 'start', 'bimivalidator.service');
}
sub update_docker_image {
remove_docker_image();
install_docker_image();
}
sub update_systemd_service {
die "Existing service not detected" unless -e '/etc/systemd/system/bimivalidator.service';
print "Stopping the running systemd service and container\n\n";
system('systemctl', 'stop', 'bimivalidator.service');
sleep 1;
system('docker', 'stop', 'bimivalidator');
sleep 2;
update_docker_image();
install_systemd_service();
}
sub main {
die "This script installs system services, please re-run as root" unless $< == 0;
die "Docker not detected, please install docker first" if `which docker` eq '';
# useful aliases
$argv = 'systemd_install' if $argv eq 'install';
$argv = 'systemd_update' if $argv eq 'update';
return install_systemd_service() if $argv eq 'systemd_install';
return update_systemd_service() if $argv eq 'systemd_update';
return install_docker_image() if $argv eq 'install_docker_image';
return update_docker_image() if $argv eq 'update_docker_image';
print "BIMI Validator helper script\n";
print "\n";
print "1) Install BIMI Validator as a systemd service\n";
print "2) Update existing BIMI Validator service\n";
print "\n";
print "0) exit\n";
print "\n";
my $input = <STDIN>;
chomp $input;
exit 0 if $input eq '0';
return install_systemd_service() if $input eq '1';
return update_systemd_service() if $input eq '2';
print "Unknown option $input\n";
exit 1;
}
main();