Skip to content

Instantly share code, notes, and snippets.

@lf94

lf94/.sml Secret

Last active September 12, 2022 14:02

Revisions

  1. lf94 revised this gist Sep 11, 2022. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion .sml
    Original file line number Diff line number Diff line change
    @@ -121,4 +121,9 @@ exec [
    ] "file" (cwd ^ "/../*")
    ]

    (* TODO: Use configuration templates for things like nginx *)
    (*
    TODO: Use configuration templates for things like nginx
    This will be simple: copy (Local [template "templates/nginx.conf" [["key","value"]]]) (Remote ["some place"])
    template will return a path to a temporary file
    It will invoke whatever templating tech you'd like.
    *)
  2. lf94 revised this gist Sep 11, 2022. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions .sml
    Original file line number Diff line number Diff line change
    @@ -120,3 +120,5 @@ exec [
    remote "cd /home/len/www/$(basename $file).git/ && git config --bool core.bare true"
    ] "file" (cwd ^ "/../*")
    ]

    (* TODO: Use configuration templates for things like nginx *)
  3. lf94 revised this gist Sep 11, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .sml
    Original file line number Diff line number Diff line change
    @@ -105,7 +105,7 @@ exec [
    (*------------------------------------------------------------------------------
    Example: self-host git projects *)

    val cwd = "/home/lee/Code/lee/git-self-host";
    val cwd = "/home/len/Code/len/git-self-host";

    exec [
    ensure (User "len") Exists,
  4. lf94 revised this gist Sep 11, 2022. 1 changed file with 27 additions and 1 deletion.
    28 changes: 27 additions & 1 deletion .sml
    Original file line number Diff line number Diff line change
    @@ -76,6 +76,13 @@ fun createSession (user, host, port)
    in (copy, ensure, remote)
    end;

    fun each statements var path =
    String.concat [
    String.concat ["for ", var, " in ", path, "; do "],
    join "; " statements,
    "; done"
    ];

    fun shell cmd = cmd;

    fun exec cmds = print (join " && " cmds);
    @@ -93,4 +100,23 @@ exec [
    shell "/home/len/Wiki/rss.sh > /home/len/Wiki/feed.xml",
    copy (Local ["/home/len/Wiki/public", "/home/len/Wiki/feed.xml", "/home/len/Wiki/index.css"])
    (Remote ["/home/len/www/"])
    ]
    ]

    (*------------------------------------------------------------------------------
    Example: self-host git projects *)

    val cwd = "/home/lee/Code/lee/git-self-host";

    exec [
    ensure (User "len") Exists,
    ensure (Software "rsync") Exists,
    ensure (Software "git") Exists,
    shell String.concat [cwd, "/rss.sh > ", cwd, "/code.xml"],
    copy (Local [cwd ^ "/code.xml", cwd ^ "/index.css"])
    (Remote ["/home/len/www/"]),
    each [
    copy (Local ["$file/.git/"])
    (Remote ["/home/len/www/$(basename $file).git"]),
    remote "cd /home/len/www/$(basename $file).git/ && git config --bool core.bare true"
    ] "file" (cwd ^ "/../*")
    ]
  5. lf94 revised this gist Sep 11, 2022. 1 changed file with 1 addition and 9 deletions.
    10 changes: 1 addition & 9 deletions .sml
    Original file line number Diff line number Diff line change
    @@ -30,15 +30,7 @@ val join = String.concatWith;
    fun cp (programInvocation:string) (srcs:string list) (dest:string)
    = join " " [programInvocation, join " " srcs, dest]
    ;

    fun exec (cmds: string list) = List.foldr (fn (a, b) => a ^ ";\n" ^ b) "" cmds;

    fun plus (a, b) = String.concat [a, " && ", b];
    infix plus;

    fun fork (a, b) = String.concat [a, " &\n", b];
    infix fork;


    datatype State = Exists | Fresh
    datatype OSObject = User of string | Directory of string | Software of string;
    datatype Location
  6. lf94 revised this gist Sep 11, 2022. 1 changed file with 18 additions and 3 deletions.
    21 changes: 18 additions & 3 deletions .sml
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,21 @@
    Write SML programs to output shell code for installations and deployments.
    Modify as needed.
    --------------------------------------------------------------------------------
    Why not just use shell scripts directly?
    The lack of a type-system in all shell scripting languages is, when you think
    about, horrific. It takes one bad input to destroy a system. With types this can
    lift the level of safety very high, as now the script will not run before it is
    considered correct. In hindsight it will be clear that all scripting languages
    should have had strong typing from the beginning.
    There is not much more to say than that. Hindley-Milner type systems allow a
    great level of type inference, which means the developer of scripts does not
    need to type everything. It's this gradient of typing which suits a whole
    spectrum of needs that is powerful.
    --------------------------------------------------------------------------------
    Utilities *)

    @@ -76,14 +91,14 @@ fun exec cmds = print (join " && " cmds);
    (*------------------------------------------------------------------------------
    Example: deploy blog to a host *)

    val target = ("root", "len.falken.directory", "31336");
    val target = ("root", "len.falken.directory", "9999");
    val (copy, ensure, remote) = createSession target;

    exec [
    ensure (User "len") Exists,
    ensure (Directory "/home/len/www/") Exists,
    ensure (Software "rsync") Exists,
    shell "/home/lee/Wiki/rss.sh > /home/lee/Wiki/feed.xml",
    copy (Local ["/home/lee/Wiki/public", "/home/lee/Wiki/feed.xml", "/home/lee/Wiki/index.css"])
    shell "/home/len/Wiki/rss.sh > /home/len/Wiki/feed.xml",
    copy (Local ["/home/len/Wiki/public", "/home/len/Wiki/feed.xml", "/home/len/Wiki/index.css"])
    (Remote ["/home/len/www/"])
    ]
  7. lf94 revised this gist Sep 11, 2022. 1 changed file with 8 additions and 11 deletions.
    19 changes: 8 additions & 11 deletions .sml
    Original file line number Diff line number Diff line change
    @@ -21,9 +21,6 @@ fun exec (cmds: string list) = List.foldr (fn (a, b) => a ^ ";\n" ^ b) "" cmds;
    fun plus (a, b) = String.concat [a, " && ", b];
    infix plus;

    fun login (user, host, port)
    = String.concat ["ssh -M -p ", port, " ", user, "@", host];

    fun fork (a, b) = String.concat [a, " &\n", b];
    infix fork;

    @@ -46,7 +43,7 @@ end

    fun createSession (user, host, port)
    = let
    fun ssh cmd
    fun remote cmd
    = String.concat ["ssh -p ", port, " ", user, "@", host, " -C '", cmd, "'"]
    ;
    fun copy (Local from) (Remote to)
    @@ -57,19 +54,19 @@ fun createSession (user, host, port)
    = ""
    ;
    fun ensure (User user) Exists
    = (ssh o String.concat) ["id ", user, " || useradd -m ", user]
    = (remote o String.concat) ["id ", user, " || useradd -m ", user]
    | ensure (Directory dir) Exists
    = (ssh o String.concat) ["[ -d \"", dir, "\" ] || mkdir -p ", dir]
    = (remote o String.concat) ["[ -d \"", dir, "\" ] || mkdir -p ", dir]
    | ensure (Directory dir) Fresh
    = (ssh o String.concat) ["rm -rf ", dir, " && mkdir -p ", dir]
    = (remote o String.concat) ["rm -rf ", dir, " && mkdir -p ", dir]
    | ensure (Software ware) Exists
    = (ssh o String.concat) ["which ", ware, " || ", PackageManager.install ware]
    = (remote o String.concat) ["which ", ware, " || ", PackageManager.install ware]
    | ensure (Software ware) Fresh
    = (ssh o String.concat) [PackageManager.install ware]
    = (remote o String.concat) [PackageManager.install ware]
    | ensure _ _
    = ""
    ;
    in (copy, ensure, ssh)
    in (copy, ensure, remote)
    end;

    fun shell cmd = cmd;
    @@ -80,7 +77,7 @@ fun exec cmds = print (join " && " cmds);
    Example: deploy blog to a host *)

    val target = ("root", "len.falken.directory", "31336");
    val (copy, ensure, ssh) = createSession target;
    val (copy, ensure, remote) = createSession target;

    exec [
    ensure (User "len") Exists,
  8. lf94 revised this gist Sep 11, 2022. 1 changed file with 13 additions and 9 deletions.
    22 changes: 13 additions & 9 deletions .sml
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    --------------------------------------------------------------------------------
    Utilities *)

    val join = String.concatWith
    val join = String.concatWith;

    fun cp (programInvocation:string) (srcs:string list) (dest:string)
    = join " " [programInvocation, join " " srcs, dest]
    @@ -72,17 +72,21 @@ fun createSession (user, host, port)
    in (copy, ensure, ssh)
    end;

    fun shell cmd = cmd;

    fun exec cmds = print (join " && " cmds);

    (*------------------------------------------------------------------------------
    Example: deploy blog to a host *)

    val target = ("root", "len.falken.directory", "31336");
    val (copy, ensure, ssh) = createSession target;

    print (
    ensure (User "joe") Exists
    plus ensure (Directory "/home/joe/www/") Fresh
    plus ensure (Software "rsync") Exists
    plus ensure (Software "rsync") Fresh
    plus copy (Local ["/home/lee/Test/1.txt", "/home/lee/Test/2.txt", "/home/lee/Test/3.txt"])
    (Remote ["/home/joe/www/"])
    )
    exec [
    ensure (User "len") Exists,
    ensure (Directory "/home/len/www/") Exists,
    ensure (Software "rsync") Exists,
    shell "/home/lee/Wiki/rss.sh > /home/lee/Wiki/feed.xml",
    copy (Local ["/home/lee/Wiki/public", "/home/lee/Wiki/feed.xml", "/home/lee/Wiki/index.css"])
    (Remote ["/home/len/www/"])
    ]
  9. lf94 revised this gist Sep 10, 2022. 1 changed file with 31 additions and 21 deletions.
    52 changes: 31 additions & 21 deletions .sml
    Original file line number Diff line number Diff line change
    @@ -10,41 +10,44 @@
    --------------------------------------------------------------------------------
    Utilities *)

    fun join (withStr: string) (srcs:string list)
    = let val (_ :: rest)
    = String.explode (List.foldl (fn (a, b) => b ^ withStr ^ a) "" srcs)
    in String.implode rest
    end
    ;
    val join = String.concatWith

    fun cp (programInvocation:string) (srcs:string list) (dest:string)
    = join " " [programInvocation, join " " srcs, dest]
    ;

    fun exec (cmds: string list) = List.foldr (fn (a, b) => a ^ ";\n" ^ b) "" cmds;

    fun plus (a, b) = a ^ " && " ^ b;
    fun plus (a, b) = String.concat [a, " && ", b];
    infix plus;

    fun login (user, host, port) = "ssh -M -p " ^ port ^ " " ^ user ^ "@" ^ host;
    fun login (user, host, port)
    = String.concat ["ssh -M -p ", port, " ", user, "@", host];

    fun fork (a, b) = a ^ " &\n" ^ b;
    fun fork (a, b) = String.concat [a, " &\n", b];
    infix fork;

    datatype Presence = Exists
    datatype State = Exists | Fresh
    datatype OSObject = User of string | Directory of string | Software of string;
    datatype Location
    = Local of string list
    | Remote of string list
    ;

    (* Change this to a prefered copy program, i.e. scp *)
    fun rsync (port:string) = cp ("rsync -P " ^ port);
    fun rsync (port:string) = cp (String.concat ["rsync -vv -e 'ssh -p ", port, "'"]);

    (* Change this to the package manager on the remote system, i.e. pacman *)
    structure PackageManager =
    struct
    val pkgman = "apt"
    fun install software = String.concat [pkgman, " install -y ", software];
    end

    fun createSession (user, host, port)
    = let
    fun ssh cmd
    = "ssh -p " ^ port ^ " " ^ user ^ "@" ^ host ^ " -C " ^ cmd
    = String.concat ["ssh -p ", port, " ", user, "@", host, " -C '", cmd, "'"]
    ;
    fun copy (Local from) (Remote to)
    = rsync port from ("'" ^ user ^ "@" ^ host ^ ":" ^ (join " " to) ^ "'")
    @@ -54,11 +57,17 @@ fun createSession (user, host, port)
    = ""
    ;
    fun ensure (User user) Exists
    = ssh "id " ^ user
    = (ssh o String.concat) ["id ", user, " || useradd -m ", user]
    | ensure (Directory dir) Exists
    = ssh "'[ -d \"" ^ dir ^ "\" ]'"
    = (ssh o String.concat) ["[ -d \"", dir, "\" ] || mkdir -p ", dir]
    | ensure (Directory dir) Fresh
    = (ssh o String.concat) ["rm -rf ", dir, " && mkdir -p ", dir]
    | ensure (Software ware) Exists
    = ssh "which " ^ ware
    = (ssh o String.concat) ["which ", ware, " || ", PackageManager.install ware]
    | ensure (Software ware) Fresh
    = (ssh o String.concat) [PackageManager.install ware]
    | ensure _ _
    = ""
    ;
    in (copy, ensure, ssh)
    end;
    @@ -69,10 +78,11 @@ end;
    val target = ("root", "len.falken.directory", "31336");
    val (copy, ensure, ssh) = createSession target;


    ensure (User "len") Exists
    plus ensure (Directory "/home/len/www") Exists
    print (
    ensure (User "joe") Exists
    plus ensure (Directory "/home/joe/www/") Fresh
    plus ensure (Software "rsync") Exists
    plus copy (Local ["/home/lee/Wiki/public", "/home/lee/Wiki/feed.xml", "/home/lee/Wiki/index.css"])
    (Remote ["/home/len/www/"])
    ;
    plus ensure (Software "rsync") Fresh
    plus copy (Local ["/home/lee/Test/1.txt", "/home/lee/Test/2.txt", "/home/lee/Test/3.txt"])
    (Remote ["/home/joe/www/"])
    )
  10. lf94 created this gist Sep 10, 2022.
    78 changes: 78 additions & 0 deletions .sml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    (*------------------------------------------------------------------------------
    REPLICARE
    recipere + deplicare
    "to receive" + "explain"
    Write SML programs to output shell code for installations and deployments.
    Modify as needed.
    --------------------------------------------------------------------------------
    Utilities *)

    fun join (withStr: string) (srcs:string list)
    = let val (_ :: rest)
    = String.explode (List.foldl (fn (a, b) => b ^ withStr ^ a) "" srcs)
    in String.implode rest
    end
    ;

    fun cp (programInvocation:string) (srcs:string list) (dest:string)
    = join " " [programInvocation, join " " srcs, dest]
    ;

    fun exec (cmds: string list) = List.foldr (fn (a, b) => a ^ ";\n" ^ b) "" cmds;

    fun plus (a, b) = a ^ " && " ^ b;
    infix plus;

    fun login (user, host, port) = "ssh -M -p " ^ port ^ " " ^ user ^ "@" ^ host;

    fun fork (a, b) = a ^ " &\n" ^ b;
    infix fork;

    datatype Presence = Exists
    datatype OSObject = User of string | Directory of string | Software of string;
    datatype Location
    = Local of string list
    | Remote of string list
    ;

    (* Change this to a prefered copy program, i.e. scp *)
    fun rsync (port:string) = cp ("rsync -P " ^ port);

    fun createSession (user, host, port)
    = let
    fun ssh cmd
    = "ssh -p " ^ port ^ " " ^ user ^ "@" ^ host ^ " -C " ^ cmd
    ;
    fun copy (Local from) (Remote to)
    = rsync port from ("'" ^ user ^ "@" ^ host ^ ":" ^ (join " " to) ^ "'")
    | copy (Remote from) (Local to)
    = rsync port ["'" ^ user ^ "@" ^ host ^ ":" ^ (join " " from) ^ "'"] (List.nth (to, 0))
    | copy _ _
    = ""
    ;
    fun ensure (User user) Exists
    = ssh "id " ^ user
    | ensure (Directory dir) Exists
    = ssh "'[ -d \"" ^ dir ^ "\" ]'"
    | ensure (Software ware) Exists
    = ssh "which " ^ ware
    ;
    in (copy, ensure, ssh)
    end;

    (*------------------------------------------------------------------------------
    Example: deploy blog to a host *)

    val target = ("root", "len.falken.directory", "31336");
    val (copy, ensure, ssh) = createSession target;


    ensure (User "len") Exists
    plus ensure (Directory "/home/len/www") Exists
    plus ensure (Software "rsync") Exists
    plus copy (Local ["/home/lee/Wiki/public", "/home/lee/Wiki/feed.xml", "/home/lee/Wiki/index.css"])
    (Remote ["/home/len/www/"])
    ;