summaryrefslogtreecommitdiff
path: root/archive/Rexfile
blob: 00bebe09b7b7e37995834bc9e1617ab4b70b3237 (plain)
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
# -*- mode: cperl -*-

# Sean's personal Rexfile
#
# Copyright (C) 2020 Sean Whitton
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use 5.028;
use strict;
use warnings;
use lib "$ENV{HOME}/src/dotfiles/perl5";
use autodie;

use File::chdir;
use File::Temp qw(tempdir tempfile);
use Local::Rex::Util;
use File::Spec::Functions qw(catfile rel2abs);
use Rex::Helper::Path;

# use these modules without importing any names to avoid clashes with
# Rex functions
use File::Path ();

=head1 GLOBAL CONFIG

=cut

use Rex -feature => ['1.4'];

# never SSH passwords
key_auth;

# globals
our $dotfiles_tar;
our $dotfiles_gpg;

# constants
use constant { MY_GPG_KEY => 'spwhitton@spwhitton.name', };

=head1 MY HOSTS

=cut

task "athena", "athena.silentflame.com" => sub {
    set me => "spwhitton";

    needs main => "dotfiles";
    as_me {
        needs main => "dotfiles"
    };
};
auth for => "athena", user => "root";

=head1 TASKS FOR HOSTS, OR TO RUN LOCALLY

=cut

# this is a more general alternative to my 'insinuate-dotfiles' script
# which should be compatible with more UNIX hosts ('insinuate-dotfiles'
# relied upon some GNU tar options on the remote)
task "dotfiles" => sub {
    file "~/dotfiles.tar",  ensure => "absent";
    file "~/spwhitton.gpg", ensure => "absent";
    return if is_dir("~/src/dotfiles/.git");

    if (LOCAL { is_dir("~/src/dotfiles/.git") }) {
        # we can upload a dotfiles tarball from localhost (like
        # 'insinuate-dotfiles' did).
        #
        # Check whether we already ran the dotfiles task during this
        # rex run, and reuse the tarball if so
        unless (defined $dotfiles_tar and -f $dotfiles_tar) {
            LOCAL {
                local $CWD
                  = tempdir(DIR => catfile($ENV{HOME}, "tmp"), CLEANUP => 1);
                system
                  qw(git clone --no-hardlinks --depth 1 -o local -b master),
                  "file://$ENV{HOME}/src/dotfiles", "src/dotfiles";
                {
                    local $CWD = "src/dotfiles";
                    system qw(git remote rm local);
                    File::Path::rmtree(".git/refs/remotes/local");
                }
                system qw(tar cf dotfiles.tar src);
                $dotfiles_tar = rel2abs("dotfiles.tar");
            };
        }
        file "~/dotfiles.tar", source => $dotfiles_tar;
        extract "~/dotfiles.tar";
        file "~/dotfiles.tar", ensure => "absent";

        # copy my gpg key over there so I can use `mr up` to securely
        # update dotfiles
        if (can_run "gpg") {
            # check if we already ran the dotfiles task during this
            # rex run, and reuse the exported key if so
            unless (defined $dotfiles_gpg and -f $dotfiles_gpg) {
                LOCAL {
                    my (undef, $filename) = tempfile(
                        DIR    => catfile($ENV{HOME}, "tmp"),
                        UNLINK => 1
                    );
                    system "gpg", "--export-options", "export-minimal",
                      "--output", $filename, "--export", MY_GPG_KEY;
                    $dotfiles_gpg = $filename;
                };
            }
            file "~/spwhitton.gpg", source => $dotfiles_gpg;
            run "gpg", ["--import", resolv_path "~/spwhitton.gpg"];
            file "~/spwhitton.gpg", ensure => "absent";
        }
    } else {
        # we need to git clone & verify (like propellor does)
        die "unimplemented";
    }

    run "~/src/dotfiles/bin/bstraph.sh";
};

1;