Skip to content

Commit

Permalink
List new releases (#141)
Browse files Browse the repository at this point in the history
* List new releases

* never return null

* Channel sequence not release sequence
  • Loading branch information
emosbaugh committed Nov 14, 2019
1 parent c8dfca4 commit 30ded45
Show file tree
Hide file tree
Showing 18 changed files with 591 additions and 165 deletions.
10 changes: 2 additions & 8 deletions ffi/airgap.go
Expand Up @@ -14,10 +14,7 @@ import (

"github.com/mholt/archiver"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
kotsscheme "github.com/replicatedhq/kots/kotskinds/client/kotsclientset/scheme"
"github.com/replicatedhq/kots/pkg/pull"
"k8s.io/client-go/kubernetes/scheme"
)

//export PullFromAirgap
Expand Down Expand Up @@ -50,15 +47,12 @@ func PullFromAirgap(socket, licenseData, airgapDir, downstream, outputFile, regi
return
}

kotsscheme.AddToScheme(scheme.Scheme)
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(licenseData), nil, nil)
license, err := loadLicense(licenseData)
if err != nil {
fmt.Printf("failed to decode license data: %s\n", err.Error())
fmt.Printf("failed to load license: %s\n", err.Error())
ffiResult = NewFFIResult(1).WithError(err)
return
}
license := obj.(*kotsv1beta1.License)

licenseFile, err := ioutil.TempFile("", "kots")
if err != nil {
Expand Down
72 changes: 72 additions & 0 deletions ffi/archive.go
@@ -0,0 +1,72 @@
package main

import (
"io/ioutil"
"os"

"github.com/mholt/archiver"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"k8s.io/client-go/kubernetes/scheme"
)

func extractArchive(rootPath, fromArchivePath string) (*archiver.TarGz, error) {
// extract the current archive to this root
tarGz := &archiver.TarGz{
Tar: &archiver.Tar{
ImplicitTopLevelFolder: false,
},
}
if err := tarGz.Unarchive(fromArchivePath, rootPath); err != nil {
return nil, err
}

return tarGz, nil
}

func readCursorFromPath(installationFilePath string) (string, error) {
_, err := os.Stat(installationFilePath)
if os.IsNotExist(err) {
return "", nil
}
if err != nil {
return "", errors.Wrap(err, "failed to open file")
}

installationData, err := ioutil.ReadFile(installationFilePath)
if err != nil {
return "", errors.Wrap(err, "failed to read update installation file")
}

decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(installationData), nil, nil)
if err != nil {
return "", errors.Wrap(err, "failed to devode installation data")
}

installation := obj.(*kotsv1beta1.Installation)
return installation.Spec.UpdateCursor, nil
}

func loadLicenseFromPath(expectedLicenseFile string) (*kotsv1beta1.License, error) {
_, err := os.Stat(expectedLicenseFile)
if err != nil {
return nil, errors.New("find license file in archive")
}
licenseData, err := ioutil.ReadFile(expectedLicenseFile)
if err != nil {
return nil, errors.Wrap(err, "read license file")
}

return loadLicense(string(licenseData))
}

func loadLicense(licenseData string) (*kotsv1beta1.License, error) {
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(licenseData), nil, nil)
if err != nil {
return nil, errors.Wrap(err, "decode license data")
}

return obj.(*kotsv1beta1.License), nil
}
64 changes: 9 additions & 55 deletions ffi/main.go
Expand Up @@ -10,8 +10,6 @@ import (
"os"
"path/filepath"

"github.com/mholt/archiver"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
kotsscheme "github.com/replicatedhq/kots/kotskinds/client/kotsclientset/scheme"
"github.com/replicatedhq/kots/pkg/pull"
Expand Down Expand Up @@ -41,49 +39,29 @@ func UpdateCheck(socket, fromArchivePath string) {
}
defer os.RemoveAll(tmpRoot)

// extract the current archive to this root
tarGz := archiver.TarGz{
Tar: &archiver.Tar{
ImplicitTopLevelFolder: false,
},
}
if err := tarGz.Unarchive(fromArchivePath, tmpRoot); err != nil {
fmt.Printf("failed to unarchive: %s\n", err.Error())
tarGz, err := extractArchive(tmpRoot, fromArchivePath)
if err != nil {
fmt.Printf("failed to extract archive: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}

beforeCursor, err := readCursorFromPath(tmpRoot)
installationFilePath := filepath.Join(tmpRoot, "upstream", "userdata", "installation.yaml")
beforeCursor, err := readCursorFromPath(installationFilePath)
if err != nil {
fmt.Printf("failed to read cursor file: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}

expectedLicenseFile := filepath.Join(tmpRoot, "upstream", "userdata", "license.yaml")
_, err = os.Stat(expectedLicenseFile)
license, err := loadLicenseFromPath(expectedLicenseFile)
if err != nil {
fmt.Printf("failed to find license file in archive\n")
ffiResult = NewFFIResult(-1).WithError(err)
return
}
licenseData, err := ioutil.ReadFile(expectedLicenseFile)
if err != nil {
fmt.Printf("failed to read license file: %s\n", err.Error())
fmt.Printf("failed to load license: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}

kotsscheme.AddToScheme(scheme.Scheme)
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(licenseData), nil, nil)
if err != nil {
fmt.Printf("failed to decode license data: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
license := obj.(*kotsv1beta1.License)

pullOptions := pull.PullOptions{
LicenseFile: expectedLicenseFile,
ConfigFile: filepath.Join(tmpRoot, "upstream", "userdata", "config.yaml"),
Expand All @@ -99,7 +77,7 @@ func UpdateCheck(socket, fromArchivePath string) {
return
}

afterCursor, err := readCursorFromPath(tmpRoot)
afterCursor, err := readCursorFromPath(installationFilePath)
if err != nil {
fmt.Printf("failed to read cursor file after update: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
Expand Down Expand Up @@ -151,7 +129,6 @@ func GetLatestLicense(socket, licenseData string) {
statusClient.end(ffiResult)
}()

kotsscheme.AddToScheme(scheme.Scheme)
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(licenseData), nil, nil)
if err != nil {
Expand Down Expand Up @@ -208,7 +185,6 @@ func GetLatestLicense(socket, licenseData string) {

//export VerifyAirgapLicense
func VerifyAirgapLicense(licenseData string) *C.char {
kotsscheme.AddToScheme(scheme.Scheme)
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(licenseData), nil, nil)
if err != nil {
Expand All @@ -225,30 +201,8 @@ func VerifyAirgapLicense(licenseData string) *C.char {
return C.CString("verified")
}

func readCursorFromPath(rootPath string) (string, error) {
installationFilePath := filepath.Join(rootPath, "upstream", "userdata", "installation.yaml")
_, err := os.Stat(installationFilePath)
if os.IsNotExist(err) {
return "", nil
}
if err != nil {
return "", errors.Wrap(err, "failed to open file")
}

installationData, err := ioutil.ReadFile(installationFilePath)
if err != nil {
return "", errors.Wrap(err, "failed to read update installation file")
}

func init() {
kotsscheme.AddToScheme(scheme.Scheme)
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(installationData), nil, nil)
if err != nil {
return "", errors.Wrap(err, "failed to devode installation data")
}

installation := obj.(*kotsv1beta1.Installation)
return installation.Spec.UpdateCursor, nil
}

func main() {}
10 changes: 2 additions & 8 deletions ffi/online.go
Expand Up @@ -9,10 +9,7 @@ import (
"path"

"github.com/mholt/archiver"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
kotsscheme "github.com/replicatedhq/kots/kotskinds/client/kotsclientset/scheme"
"github.com/replicatedhq/kots/pkg/pull"
"k8s.io/client-go/kubernetes/scheme"
)

//export PullFromLicense
Expand All @@ -29,15 +26,12 @@ func PullFromLicense(socket string, licenseData string, downstream string, outpu
statusClient.end(ffiResult)
}()

kotsscheme.AddToScheme(scheme.Scheme)
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(licenseData), nil, nil)
license, err := loadLicense(licenseData)
if err != nil {
fmt.Printf("failed to decode license data: %s\n", err.Error())
fmt.Printf("failed to load license: %s\n", err.Error())
ffiResult = NewFFIResult(1).WithError(err)
return
}
license := obj.(*kotsv1beta1.License)

licenseFile, err := ioutil.TempFile("", "kots")
if err != nil {
Expand Down
111 changes: 111 additions & 0 deletions ffi/updatedownload.go
@@ -0,0 +1,111 @@
package main

import "C"

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/replicatedhq/kots/pkg/pull"
)

//export UpdateDownload
func UpdateDownload(socket, fromArchivePath, cursor string) {
go func() {
var ffiResult *FFIResult

statusClient, err := connectToStatusServer(socket)
if err != nil {
fmt.Printf("failed to connect to status server: %s\n", err)
return
}
defer func() {
statusClient.end(ffiResult)
}()

tmpRoot, err := ioutil.TempDir("", "kots")
if err != nil {
fmt.Printf("failed to create temp path: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
defer os.RemoveAll(tmpRoot)

tarGz, err := extractArchive(tmpRoot, fromArchivePath)
if err != nil {
fmt.Printf("failed to extract archive: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}

installationFilePath := filepath.Join(tmpRoot, "upstream", "userdata", "installation.yaml")
beforeCursor, err := readCursorFromPath(installationFilePath)
if err != nil {
fmt.Printf("failed to read cursor file: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}

expectedLicenseFile := filepath.Join(tmpRoot, "upstream", "userdata", "license.yaml")
license, err := loadLicenseFromPath(expectedLicenseFile)
if err != nil {
fmt.Printf("failed to load license: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}

pullOptions := pull.PullOptions{
LicenseFile: expectedLicenseFile,
ConfigFile: filepath.Join(tmpRoot, "upstream", "userdata", "config.yaml"),
UpdateCursor: cursor,
RootDir: tmpRoot,
ExcludeKotsKinds: true,
ExcludeAdminConsole: true,
CreateAppDir: false,
}

if _, err := pull.Pull(fmt.Sprintf("replicated://%s", license.Spec.AppSlug), pullOptions); err != nil {
fmt.Printf("failed to pull upstream: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}

afterCursor, err := readCursorFromPath(installationFilePath)
if err != nil {
fmt.Printf("failed to read cursor file after update: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}

fmt.Printf("Result of checking for updates for %s: Before: %s, After %s\n", license.Spec.AppSlug, beforeCursor, afterCursor)

isUpdateAvailable := string(beforeCursor) != string(afterCursor)
if !isUpdateAvailable {
ffiResult = NewFFIResult(0)
return
}

paths := []string{
filepath.Join(tmpRoot, "upstream"),
filepath.Join(tmpRoot, "base"),
filepath.Join(tmpRoot, "overlays"),
}

err = os.Remove(fromArchivePath)
if err != nil {
fmt.Printf("failed to delete archive to replace: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}

if err := tarGz.Archive(paths, fromArchivePath); err != nil {
fmt.Printf("failed to write archive: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}

ffiResult = NewFFIResult(1)
}()
}

0 comments on commit 30ded45

Please sign in to comment.