2018-02-02 17:36:48 +01:00
package shared
import (
2018-02-14 17:59:39 +01:00
"fmt"
2018-02-02 17:36:48 +01:00
"io"
"os"
"os/exec"
2018-02-14 17:59:39 +01:00
"path/filepath"
2018-02-22 16:34:47 +01:00
"regexp"
"strconv"
"time"
2018-02-14 17:59:39 +01:00
lxd "github.com/lxc/lxd/shared"
2018-02-02 17:36:48 +01:00
)
// Copy copies a file.
func Copy ( src , dest string ) error {
var err error
srcFile , err := os . Open ( src )
if err != nil {
return err
}
defer srcFile . Close ( )
destFile , err := os . Create ( dest )
if err != nil {
return err
}
defer destFile . Close ( )
_ , err = io . Copy ( destFile , srcFile )
if err != nil {
return err
}
return destFile . Sync ( )
}
// RunCommand runs a command hereby setting the SHELL and PATH env variables,
// and redirecting the process's stdout and stderr to the real stdout and stderr
// respectively.
func RunCommand ( name string , arg ... string ) error {
cmd := exec . Command ( name , arg ... )
2018-02-21 19:28:28 +01:00
cmd . Env = [ ] string {
"PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin" ,
"SHELL=/bin/sh" ,
"TERM=xterm" }
2018-02-02 17:36:48 +01:00
cmd . Stdin = os . Stdin
cmd . Stdout = os . Stdout
cmd . Stderr = os . Stderr
return cmd . Run ( )
}
2018-02-14 17:59:39 +01:00
// VerifyFile verifies a file using gpg.
2018-02-16 16:05:42 +01:00
func VerifyFile ( signedFile , signatureFile string , keys [ ] string , keyserver string ) ( bool , error ) {
2018-02-14 17:59:39 +01:00
var out string
gpgDir := filepath . Join ( os . TempDir ( ) , "distrobuilder.gpg" )
err := os . MkdirAll ( gpgDir , 0700 )
if err != nil {
return false , err
}
defer os . RemoveAll ( gpgDir )
2018-02-16 16:05:42 +01:00
out , err = lxd . RunCommand ( "gpg" , append ( [ ] string {
"--homedir" , gpgDir , "--keyserver" , keyserver , "--recv-keys" } , keys ... ) ... )
2018-02-14 17:59:39 +01:00
if err != nil {
return false , fmt . Errorf ( "Failed to receive keys: %s" , out )
}
if signatureFile != "" {
out , err = lxd . RunCommand ( "gpg" , "--homedir" , gpgDir , "--verify" , signatureFile , signedFile )
if err != nil {
return false , fmt . Errorf ( "Failed to verify: %s" , out )
}
} else {
out , err = lxd . RunCommand ( "gpg" , "--homedir" , gpgDir , "--verify" , signedFile )
if err != nil {
return false , fmt . Errorf ( "Failed to verify: %s" , out )
}
}
return true , nil
}
2018-02-12 16:19:29 +01:00
// Pack creates an xz-compressed tarball.
func Pack ( filename , path string , args ... string ) error {
return RunCommand ( "tar" , append ( [ ] string { "-cJf" , filename , "-C" , path } , args ... ) ... )
}
2018-02-22 16:34:47 +01:00
//GetExpiryDate returns an expiry date based on the creationDate and format.
func GetExpiryDate ( creationDate time . Time , format string ) time . Time {
regex := regexp . MustCompile ( ` (?:(\d+)(s|m|h|d|w))* ` )
expiryDate := creationDate
for _ , match := range regex . FindAllStringSubmatch ( format , - 1 ) {
// Ignore empty matches
if match [ 0 ] == "" {
continue
}
var duration time . Duration
switch match [ 2 ] {
case "s" :
duration = time . Second
case "m" :
duration = time . Minute
case "h" :
duration = time . Hour
case "d" :
duration = 24 * time . Hour
case "w" :
duration = 7 * 24 * time . Hour
}
// Ignore any error since it will be an integer.
value , _ := strconv . Atoi ( match [ 1 ] )
expiryDate = expiryDate . Add ( time . Duration ( value ) * duration )
}
return expiryDate
}