Skip to content

Commit

Permalink
Merge branch 'master' into delete-team-fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Cosmin Cojocar committed Dec 17, 2018
2 parents 98fb763 + c01a726 commit b75559c
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 147 deletions.
13 changes: 7 additions & 6 deletions Jenkinsfile
Expand Up @@ -3,12 +3,13 @@ pipeline {
environment {
CHARTMUSEUM_CREDS = credentials('jenkins-x-chartmuseum')
JENKINS_CREDS = credentials('test-jenkins-user')
GH_CREDS = credentials('jenkins-x-github')
GHE_CREDS = credentials('ghe-test-user')
GH_CREDS = credentials('jx-pipeline-git-github-github')
GHE_CREDS = credentials('jx-pipeline-git-github-ghe')
GKE_SA = credentials('gke-sa')
BUILD_NUMBER = "${JX_BUILD_NUMBER ?: '1'}"
GIT_USERNAME = "$GH_CREDS_USR"
GIT_API_TOKEN = "$GH_CREDS_PSW"

GIT_USERNAME = "$GH_CREDS_USR"
GIT_API_TOKEN = "$GH_CREDS_PSW"
GITHUB_ACCESS_TOKEN = "$GH_CREDS_PSW"

JOB_NAME = "$JOB_NAME"
Expand All @@ -34,6 +35,8 @@ pipeline {
steps {
dir ('/home/jenkins/go/src/github.com/jenkins-x/jx') {
checkout scm
sh "git config --global credential.helper store"
sh "jx step git credentials"

sh "echo building Pull Request for preview ${TEAM}"

Expand All @@ -44,8 +47,6 @@ pipeline {

sh "docker build -t docker.io/$ORG/$APP_NAME:$PREVIEW_VERSION ."

sh "jx step git credentials"

sh "make preview"

// lets create a team for this PR and run the BDD tests
Expand Down
33 changes: 25 additions & 8 deletions pkg/jx/cmd/common_install.go
Expand Up @@ -848,7 +848,7 @@ func (o *CommonOptions) installHelmSecretsPlugin(helmBinary string, clientOnly b
Args: []string{"plugin", "remove", "secrets"},
}
_, err = cmd.RunWithoutRetry()
if err != nil && !strings.Contains(err.Error(),"secrets not found") {
if err != nil && !strings.Contains(err.Error(), "secrets not found") {
return errors.Wrap(err, "failed to remove helm secrets")
}
cmd = util.Command{
Expand Down Expand Up @@ -1020,19 +1020,36 @@ func (o *CommonOptions) installKops() error {
return os.Chmod(fullPath, 0755)
}

func (o *CommonOptions) installKSync() (bool, error) {
func (o *CommonOptions) installKSync() (string, error) {
binDir, err := util.JXBinLocation()
if err != nil {
return false, err
return "", err
}
binary := "ksync"
fileName, flag, err := shouldInstallBinary(binary)
if err != nil || !flag {
return false, err
// Exec `ksync` to find the version
ksyncCmd := util.Command{
Name: fileName,
Args: []string{
"version",
},
}
// Explicitly ignore any errors from ksync version, as we just need the output!
res, _ := ksyncCmd.RunWithoutRetry()
lines := strings.Split(res, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "Git Tag:") {
return strings.TrimSpace(strings.TrimPrefix(line, "Git Tag:")), nil
}
}

return "", fmt.Errorf("unable to find version of ksync")
}
latestVersion, err := util.GetLatestVersionFromGitHub("vapor-ware", "ksync")
if err != nil {
return false, err
return "", err
}
clientURL := fmt.Sprintf("https://github.com/vapor-ware/ksync/releases/download/%s/ksync_%s_%s", latestVersion, runtime.GOOS, runtime.GOARCH)
if runtime.GOOS == "windows" {
Expand All @@ -1042,13 +1059,13 @@ func (o *CommonOptions) installKSync() (bool, error) {
tmpFile := fullPath + ".tmp"
err = binaries.DownloadFile(clientURL, tmpFile)
if err != nil {
return false, err
return "", err
}
err = util.RenameFile(tmpFile, fullPath)
if err != nil {
return false, err
return "", err
}
return true, os.Chmod(fullPath, 0755)
return latestVersion.String(), os.Chmod(fullPath, 0755)
}

func (o *CommonOptions) installJx(upgrade bool, version string) error {
Expand Down
15 changes: 15 additions & 0 deletions pkg/jx/cmd/controller_commitstatus.go
Expand Up @@ -224,6 +224,8 @@ func (o *ControllerCommitStatusOptions) onPod(pod *corev1.Pod, jxClient jenkinsv
pullPullSha := ""
pullBaseSha := ""
buildNumber := ""
jxBuildNumber := ""
buildId := ""
sourceUrl := ""
branch := ""
for _, initContainer := range pod.Spec.InitContainers {
Expand All @@ -240,7 +242,11 @@ func (o *ControllerCommitStatusOptions) onPod(pod *corev1.Pod, jxClient jenkinsv
case "PULL_BASE_SHA":
pullBaseSha = e.Value
case "JX_BUILD_NUMBER":
jxBuildNumber = e.Value
case "BUILD_NUMBER":
buildNumber = e.Value
case "BUILD_ID":
buildId = e.Value
case "SOURCE_URL":
sourceUrl = e.Value
case "PULL_BASE_REF":
Expand All @@ -256,6 +262,15 @@ func (o *ControllerCommitStatusOptions) onPod(pod *corev1.Pod, jxClient jenkinsv
sha = pullPullSha
branch = pullRequest
}

// if BUILD_ID is set, use it, otherwise if JX_BUILD_NUMBER is set, use it, otherwise use BUILD_NUMBER
if jxBuildNumber != "" {
buildNumber = jxBuildNumber
}
if buildId != "" {
buildNumber = buildId
}

pipelineActName := kube.ToValidName(fmt.Sprintf("%s-%s-%s-%s", org, repo, branch, buildNumber))

// PLM TODO This is a bit of hack, we need a working build controller
Expand Down
4 changes: 2 additions & 2 deletions pkg/jx/cmd/create_devpod.go
Expand Up @@ -676,8 +676,8 @@ func (o *CreateDevPodOptions) Run() error {
log.Infof("Attempting to install Bash Completion into DevPod\n")

rshExec = append(rshExec,
"if which yum 1> /dev/null; then yum install -q -y bash-completion bash-completion-extra; fi",
"if which apt-get 1> /dev/null; then apt-get install -qq bash-completion; fi",
"if which yum &> /dev/null; then yum install -q -y bash-completion bash-completion-extra; fi",
"if which apt-get &> /dev/null; then apt-get install -qq bash-completion; fi",
"mkdir -p ~/.jx", "jx completion bash > ~/.jx/bash", "echo \"source ~/.jx/bash\" >> ~/.bashrc",
)

Expand Down
60 changes: 0 additions & 60 deletions pkg/jx/cmd/create_git_api_token.go
Expand Up @@ -4,22 +4,18 @@ import (
"context"
"fmt"
"io"
"net/url"
"strings"
"time"

"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/chromedp"
"github.com/jenkins-x/jx/pkg/auth"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/nodes"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/uuid"
)

Expand Down Expand Up @@ -155,11 +151,6 @@ func (o *CreateGitTokenOptions) Run() error {
return err
}

err = o.updateGitCredentialsSecret(server, userAuth)
if err != nil {
log.Warnf("Failed to update Jenkins Git credentials secret: %v\n", err)
}

_, err = o.updatePipelineGitCredentialsSecret(server, userAuth)
if err != nil {
log.Warnf("Failed to update Jenkins X pipeline Git credentials secret: %v\n", err)
Expand Down Expand Up @@ -265,54 +256,3 @@ func (o *CreateGitTokenOptions) tryFindAPITokenFromBrowser(tokenUrl string, user
}
return nil
}

func (o *CreateGitTokenOptions) updateGitCredentialsSecret(server *auth.AuthServer, userAuth *auth.UserAuth) error {
client, ns, err := o.KubeClient()
if err != nil {
return err
}
options := metav1.GetOptions{}
secret, err := client.CoreV1().Secrets(ns).Get(kube.SecretJenkinsGitCredentials, options)
if err != nil {
// lets try the real dev namespace if we are in a different environment
devNs, _, err := kube.GetDevNamespace(client, ns)
if err != nil {
return err
}
secret, err = client.CoreV1().Secrets(devNs).Get(kube.SecretJenkinsGitCredentials, options)
if err != nil {
return err
}
}
text := ""
data := secret.Data[jenkinsGitCredentialsSecretKey]
if data != nil {
text = string(data)
}
lines := strings.Split(text, "\n")
u, err := url.Parse(server.URL)
if err != nil {
return fmt.Errorf("Failed to parse server URL %s due to: %s", server.URL, err)
}

found := false
prefix := u.Scheme + "://"
host := u.Host
for _, line := range lines {
if strings.HasPrefix(line, prefix) && strings.HasSuffix(line, host) {
found = true
}
}
if !found {
if !strings.HasSuffix(text, "\n") {
text += "\n"
}
text += prefix + userAuth.Username + ":" + userAuth.ApiToken + "@" + host
secret.Data[jenkinsGitCredentialsSecretKey] = []byte(text)
_, err = client.CoreV1().Secrets(ns).Update(secret)
if err != nil {
return fmt.Errorf("Failed to update secret %s due to %s", secret.Name, err)
}
}
return nil
}
52 changes: 5 additions & 47 deletions pkg/jx/cmd/install.go
Expand Up @@ -117,7 +117,6 @@ const (
JenkinsXPlatformChart = "jenkins-x/" + JenkinsXPlatformChartName
JenkinsXPlatformRelease = "jenkins-x"

GitSecretsFile = "gitSecrets.yaml"
AdminSecretsFile = "adminSecrets.yaml"
ExtraValuesFile = "extraValues.yaml"
JXInstallConfig = "jx-install-config"
Expand Down Expand Up @@ -975,23 +974,11 @@ func (options *InstallOptions) getHelmValuesFiles(configStore configio.ConfigSto
errors.Wrap(err, "creating the admin secrets")
}

gitSecrets, err := options.getGitSecrets()
if err != nil {
return valuesFiles, secretsFiles, temporaryFiles,
errors.Wrap(err, "reading the git secrets from configuration")
}

dir, err := util.ConfigDir()
if err != nil {
return valuesFiles, secretsFiles, temporaryFiles,
errors.Wrap(err, "creating a temporary config dir for Git credentials")
}
gitSecretsFileName := filepath.Join(dir, GitSecretsFile)
err = configStore.Write(gitSecretsFileName, []byte(gitSecrets))
if err != nil {
return valuesFiles, secretsFiles, temporaryFiles,
errors.Wrapf(err, "writing the git secrets in the secrets file '%s'", gitSecretsFileName)
}

adminSecretsFileName := filepath.Join(dir, AdminSecretsFile)
err = configStore.WriteObject(adminSecretsFileName, adminSecrets)
Expand All @@ -1008,7 +995,7 @@ func (options *InstallOptions) getHelmValuesFiles(configStore configio.ConfigSto
}
log.Infof("Generated helm values %s\n", util.ColorInfo(extraValuesFileName))

err = options.modifySecrets(helmConfig, adminSecrets, gitSecrets)
err = options.modifySecrets(helmConfig, adminSecrets)
if err != nil {
return valuesFiles, temporaryFiles, secretsFiles, errors.Wrap(err, "updating the secrets data in Kubernetes cluster")
}
Expand All @@ -1020,17 +1007,17 @@ func (options *InstallOptions) getHelmValuesFiles(configStore configio.ConfigSto
errors.Wrap(err, "failed to append the myvalues.yaml file")
}
secretsFiles = append(secretsFiles,
[]string{gitSecretsFileName, adminSecretsFileName, extraValuesFileName, cloudEnvironmentSecretsLocation}...)
[]string{adminSecretsFileName, extraValuesFileName, cloudEnvironmentSecretsLocation}...)

if options.Flags.Vault {
temporaryFiles = append(temporaryFiles, adminSecretsFileName, gitSecretsFileName, extraValuesFileName, cloudEnvironmentSecretsLocation)
temporaryFiles = append(temporaryFiles, adminSecretsFileName, extraValuesFileName, cloudEnvironmentSecretsLocation)
err := options.storeSecretsFilesInVault([]string{adminSecretsFileName})
if err != nil {
return valuesFiles, secretsFiles, temporaryFiles,
errors.Wrapf(err, "storing in Vault the secrets files: %s", adminSecretsFileName)
}
} else {
temporaryFiles = append(temporaryFiles, gitSecretsFileName, extraValuesFileName, cloudEnvironmentSecretsLocation)
temporaryFiles = append(temporaryFiles, extraValuesFileName, cloudEnvironmentSecretsLocation)
}

return util.FilterFileExists(valuesFiles), util.FilterFileExists(secretsFiles), util.FilterFileExists(temporaryFiles), nil
Expand Down Expand Up @@ -2032,7 +2019,7 @@ func (options *InstallOptions) createEnvironments(namespace string) error {
return nil
}

func (options *InstallOptions) modifySecrets(helmConfig *config.HelmValuesConfig, adminSecrets *config.AdminSecretsConfig, gitSecrets string) error {
func (options *InstallOptions) modifySecrets(helmConfig *config.HelmValuesConfig, adminSecrets *config.AdminSecretsConfig) error {
var err error
data := make(map[string][]byte)
data[ExtraValuesFile], err = yaml.Marshal(helmConfig)
Expand All @@ -2043,7 +2030,6 @@ func (options *InstallOptions) modifySecrets(helmConfig *config.HelmValuesConfig
if err != nil {
return err
}
data[GitSecretsFile] = []byte(gitSecrets)
_, err = options.ModifySecret(JXInstallConfig, func(secret *core_v1.Secret) error {
secret.Data = data
return nil
Expand Down Expand Up @@ -2341,34 +2327,6 @@ func (options *InstallOptions) cloneJXCloudEnvironmentsRepo() (string, error) {
return wrkDir, nil
}

// returns secrets that are used as values during the helm install
func (options *InstallOptions) getGitSecrets() (string, error) {
pipelineServer, pipelineUser, err := options.getPipelineGitAuth()
if err != nil {
return "", errors.Wrap(err, "retrieving the pipeline Git Auth")
}
if pipelineServer == nil {
return "", errors.New("no pipeline Git server found")
}

if pipelineUser == nil {
return "", errors.New("no pipeline Git user found")
}

serverURL := pipelineServer.URL
server := strings.TrimPrefix(serverURL, "https://")
server = strings.TrimPrefix(server, "http://")

url := fmt.Sprintf("%s:%s@%s", pipelineUser.Username, pipelineUser.ApiToken, server)

pipelineSecrets := `
PipelineSecrets:
GitCreds: |-
https://%s
http://%s`
return fmt.Sprintf(pipelineSecrets, url, url), nil
}

func (options *InstallOptions) getPipelineGitAuth() (*auth.AuthServer, *auth.UserAuth, error) {
authConfigSvc, err := options.CreateGitAuthConfigService()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/jx/cmd/step_git.go
Expand Up @@ -44,6 +44,7 @@ func NewCmdStepGit(f Factory, in terminal.FileReader, out terminal.FileWriter, e
},
}
cmd.AddCommand(NewCmdStepGitCredentials(f, in, out, errOut))
cmd.AddCommand(NewCmdStepGitEnvs(f, in, out, errOut))
return cmd
}

Expand Down

0 comments on commit b75559c

Please sign in to comment.