Skip to content
Snippets Groups Projects
Unverified Commit a22892f4 authored by Massimiliano Adamo's avatar Massimiliano Adamo
Browse files

add update function

parent 78286263
Branches
Tags
No related merge requests found
...@@ -5,6 +5,7 @@ go 1.16 ...@@ -5,6 +5,7 @@ go 1.16
require ( require (
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
github.com/go-ini/ini v1.62.0 github.com/go-ini/ini v1.62.0
github.com/hashicorp/go-version v1.3.0
github.com/smartystreets/goconvey v1.6.4 // indirect github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/tidwall/gjson v1.8.0 github.com/tidwall/gjson v1.8.0
gopkg.in/ini.v1 v1.62.0 // indirect gopkg.in/ini.v1 v1.62.0 // indirect
......
...@@ -4,6 +4,8 @@ github.com/go-ini/ini v1.62.0 h1:7VJT/ZXjzqSrvtraFp4ONq80hTcRQth1c9ZnQ3uNQvU= ...@@ -4,6 +4,8 @@ github.com/go-ini/ini v1.62.0 h1:7VJT/ZXjzqSrvtraFp4ONq80hTcRQth1c9ZnQ3uNQvU=
github.com/go-ini/ini v1.62.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/go-ini/ini v1.62.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw=
github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
......
...@@ -3,6 +3,7 @@ package main ...@@ -3,6 +3,7 @@ package main
import ( import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"encoding/json"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"io" "io"
...@@ -18,12 +19,22 @@ import ( ...@@ -18,12 +19,22 @@ import (
"github.com/docopt/docopt-go" "github.com/docopt/docopt-go"
"github.com/go-ini/ini" "github.com/go-ini/ini"
"github.com/hashicorp/go-version"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
) )
const errMsg string = "[ERR]" const errMsg string = "[ERR]"
const infoMsg string = "[INFO]" const infoMsg string = "[INFO]"
type Properties struct {
Version []string `json:"version"`
}
type JsonArtifact struct {
Properties Properties
Uri string
}
var ( var (
appVersion string appVersion string
buildTime string buildTime string
...@@ -49,6 +60,7 @@ var ( ...@@ -49,6 +60,7 @@ var (
certTmpDir string certTmpDir string
key *x509.Certificate key *x509.Certificate
cert *x509.Certificate cert *x509.Certificate
httpClient = &http.Client{Timeout: 10 * time.Second}
) )
// app clean and exit // app clean and exit
...@@ -65,6 +77,22 @@ func appExit(status int) { ...@@ -65,6 +77,22 @@ func appExit(status int) {
os.Exit(status) os.Exit(status)
} }
// get upstream version
func getUpstreamVersion(url string) string {
resp, err := httpClient.Get(url)
if err != nil {
fmt.Printf("Request to Artifactory failed: %v\n", err)
appExit(255)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Reading body from Artifactory failed: %v\n", err)
appExit(255)
}
return string(body)
}
// check certificates // check certificates
func checkCertificates(dnsname string, certificate string, fullchain string, ca string, key string, days int, fail bool) bool { func checkCertificates(dnsname string, certificate string, fullchain string, ca string, key string, days int, fail bool) bool {
...@@ -331,6 +359,7 @@ func downloadFile(filepath string, url string) (err error) { ...@@ -331,6 +359,7 @@ func downloadFile(filepath string, url string) (err error) {
func main() { func main() {
progName := filepath.Base(os.Args[0])
OSInfo := ReadOSRelease("/etc/os-release") OSInfo := ReadOSRelease("/etc/os-release")
OSRelease := OSInfo["ID"] OSRelease := OSInfo["ID"]
if OSRelease == "centos" || OSRelease == "rhel" { if OSRelease == "centos" || OSRelease == "rhel" {
...@@ -361,11 +390,12 @@ func main() { ...@@ -361,11 +390,12 @@ func main() {
- fetches and stores a given Certificate, Full Chain, CA and Private Key - fetches and stores a given Certificate, Full Chain, CA and Private Key
Usage: Usage:
acme-downloader --redis-token=REDISTOKEN --vault-token=VAULTTOKEN --cert-name=CERTNAME --team-name=TEAMNAME [--silent] [--days=DAYS] [--type=TYPE] [--cert-destination=CERTDESTINATION] [--fullchain-destination=FULLCHAINDESTINATION] [--key-destination=KEYDESTINATION] [--ca-destination=CADESTINATION] [--wildcard] %v --redis-token=REDISTOKEN --vault-token=VAULTTOKEN --cert-name=CERTNAME --team-name=TEAMNAME [--silent] [--days=DAYS] [--type=TYPE] [--cert-destination=CERTDESTINATION] [--fullchain-destination=FULLCHAINDESTINATION] [--key-destination=KEYDESTINATION] [--ca-destination=CADESTINATION] [--wildcard]
acme-downloader -h | --help %v -h | --help
acme-downloader -v | --version %v -v | --version
acme-downloader -b | --build %v -b | --build
acme-downloader --update %v --update
%v --check-version
Options: Options:
-h --help Show this screen -h --help Show this screen
...@@ -383,15 +413,41 @@ Options: ...@@ -383,15 +413,41 @@ Options:
--ca-destination=CADESTINATION CA Destination [default: %v/COMODO_<type>.crt] --ca-destination=CADESTINATION CA Destination [default: %v/COMODO_<type>.crt]
--wildcard The certificate type is wildcard --wildcard The certificate type is wildcard
--update Self-updates the tool and exit --update Self-updates the tool and exit
`, CertBase, CertBase, KeyBase, CertBase) --check-version Check upstream version
`, progName, progName, progName, progName, progName, progName, CertBase, CertBase, KeyBase, CertBase)
arguments, _ := docopt.Parse(usage, nil, true, appVersion, false) arguments, _ := docopt.Parse(usage, nil, true, appVersion, false)
if arguments["--build"] == true { if arguments["--build"] == true {
fmt.Printf("acme-downloader version: %v, built on: %v\n", appVersion, buildTime) fmt.Printf("%v version: %v, built on: %v\n", progName, appVersion, buildTime)
appExit(0)
}
// chekc upstream version
metadataBase := "https://artifactory.software.geant.org/artifactory/api/storage/acme-downloader/acme-downloader"
metadataURL := fmt.Sprintf("%v_%v_%v?properties=version", metadataBase, runtime.GOOS, runtime.GOARCH)
if runtime.GOOS == "windows" {
metadataURL = fmt.Sprintf("%v_%v_%v.exe?properties=version", metadataBase, runtime.GOOS, runtime.GOARCH)
}
upstreamResp := getUpstreamVersion(metadataURL)
var jsonartifact JsonArtifact
json.Unmarshal([]byte(upstreamResp), &jsonartifact)
upstreamVersion := fmt.Sprintf(jsonartifact.Properties.Version[0])
localVersion, _ := version.NewVersion(appVersion)
remoteVersion, _ := version.NewVersion(upstreamVersion)
if localVersion.GreaterThan(remoteVersion) {
fmt.Printf("local version (%s) is older than upstream version (%s)\n", localVersion, remoteVersion)
fmt.Printf("please use --update to install the new version\n")
} else {
if arguments["--check-version"] == true {
fmt.Printf("You are running the latest version (%v)\n", appVersion)
}
}
if arguments["--check-version"] == true {
appExit(0) appExit(0)
} }
// fetch and install the upstream version
if arguments["--update"] == true { if arguments["--update"] == true {
ArtifactoryBase := "https://artifactory.software.geant.org/artifactory/acme-downloader" ArtifactoryBase := "https://artifactory.software.geant.org/artifactory/acme-downloader"
ArtifactName := fmt.Sprintf("acme-downloader_%v_%v", runtime.GOOS, runtime.GOARCH) ArtifactName := fmt.Sprintf("acme-downloader_%v_%v", runtime.GOOS, runtime.GOARCH)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment