<commit_before>#!/bin/sh

log()   { echo ">>> $1" ; }
abort() { echo ">>> FATAL: $1" ; exit 1 ; }

HOSTNAME=
FINISH=
DELETE_SALT_KEYS=

# the hostname and port where the API server will be listening at
API_SERVER_DNS_NAME="kube-master"
API_SERVER_PORT=6443

while [[ $# > 0 ]] ; do
  case $1 in
    --debug)
      set -x
      ;;
    -F|--finish)
      FINISH=1
      ;;
    -D|--delete-keys)
      DELETE_SALT_KEYS=1
      ;;
    -h|--hostname)
      HOSTNAME=$2
      shift
      ;;
    --extra-api-ip)
      export EXTRA_API_SRV_IP=$2
      shift
      ;;
    --api-server-name)
      API_SERVER_DNS_NAME=$2
      shift
      ;;
    *)
      abort "Unknown argument $1"
      ;;
  esac
  shift
done

###################################################################

if [ "$FINISH" != "1" ] ; then
    log "Fix the ssh keys permissions and set the authorized keys"
    chmod 600 /root/.ssh/*
    cp -f /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys

    log "Upgrading the Salt master"
    zypper -n --no-gpg-checks in --force-resolution --no-recommends salt-master
    cp -v /tmp/salt/master.d/* /etc/salt/master.d

    log "Fixing some permissions"
    [ -f /srv/salt/certs/certs.sh ] && chmod 755 /srv/salt/certs/certs.sh
    [ -d /srv/files ] || mkdir -p /srv/files

    if [ -n "$HOSTNAME" ] ; then
        log "Setting hostname $HOSTNAME"
        hostname $HOSTNAME
    fi

    log "Enabling & starting the Salt master"
    systemctl enable salt-master
    systemctl start salt-master

    if [ "$DELETE_SALT_KEYS" = "1" ] ; then
        sleep 5
        log "Removing all previous Salt keys..."
        /usr/bin/salt-key --delete-all --yes || /bin/true
    fi
else
    log "Running certs.sh in the Salt master"
    /srv/salt/certs/certs.sh

    log "Running the orchestration in the Salt master"
    salt-run state.orchestrate orch.kubernetes

    # dirs in the salt master
    CA_DIR=/srv/files
    OUT_DIR=/root

    # needed for the "host" utility
    zypper -n --no-gpg-checks in --force-resolution --no-recommends bind-utils

    API_SERVER_IP=$(host $API_SERVER_DNS_NAME | grep "has address" | awk '{print $NF}')
    [ -n "$API_SERVER_IP" ] || abort "could not determine the IP of the API server with DNS"
    [ -f $CA_DIR/ca.crt ]   || abort "CA file does not exist"

    log "Generating certificates for 'kubectl' in the Salt master"
    [ -f $OUT_DIR/ca.crt ]    || cp $CA_DIR/ca.crt $OUT_DIR/ca.crt
    [ -f $OUT_DIR/admin.key ] || openssl genrsa -out $OUT_DIR/admin.key 2048
    [ -f $OUT_DIR/admin.csr ] || openssl req -new -key $OUT_DIR/admin.key \
        -out $OUT_DIR/admin.csr -subj "/CN=kube-admin"
    [ -f $OUT_DIR/admin.crt ] || openssl x509 -req \
        -in $OUT_DIR/admin.csr -CA $CA_DIR/ca.crt \
        -CAkey $CA_DIR/ca.key -CAcreateserial \
        -out $OUT_DIR/admin.crt -days 365

    log "Generating a 'kubeconfig' file"
    cat <<EOF > $OUT_DIR/kubeconfig
apiVersion: v1
clusters:
- cluster:
    certificate-authority: ca.crt
    server: https://${API_SERVER_IP}:${API_SERVER_PORT}/
  name: default-cluster
contexts:
- context:<commit_msg>Use the API server IP (when provided) for generating the kubeconfig<commit_after>#!/bin/sh

log()   { echo ">>> $1" ; }
abort() { echo ">>> FATAL: $1" ; exit 1 ; }

HOSTNAME=
FINISH=
DELETE_SALT_KEYS=

# the hostname and port where the API server will be listening at
API_SERVER_DNS_NAME="kube-master"
API_SERVER_PORT=6443

while [[ $# > 0 ]] ; do
  case $1 in
    --debug)
      set -x
      ;;
    -F|--finish)
      FINISH=1
      ;;
    -D|--delete-keys)
      DELETE_SALT_KEYS=1
      ;;
    -h|--hostname)
      HOSTNAME=$2
      shift
      ;;
    --extra-api-ip)
      export EXTRA_API_SRV_IP=$2
      shift
      ;;
    --api-server-name)
      API_SERVER_DNS_NAME=$2
      shift
      ;;
    *)
      abort "Unknown argument $1"
      ;;
  esac
  shift
done

###################################################################

if [ "$FINISH" != "1" ] ; then
    log "Fix the ssh keys permissions and set the authorized keys"
    chmod 600 /root/.ssh/*
    cp -f /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys

    log "Upgrading the Salt master"
    zypper -n --no-gpg-checks in --force-resolution --no-recommends salt-master
    cp -v /tmp/salt/master.d/* /etc/salt/master.d

    log "Fixing some permissions"
    [ -f /srv/salt/certs/certs.sh ] && chmod 755 /srv/salt/certs/certs.sh
    [ -d /srv/files ] || mkdir -p /srv/files

    if [ -n "$HOSTNAME" ] ; then
        log "Setting hostname $HOSTNAME"
        hostname $HOSTNAME
    fi

    log "Enabling & starting the Salt master"
    systemctl enable salt-master
    systemctl start salt-master

    if [ "$DELETE_SALT_KEYS" = "1" ] ; then
        sleep 5
        log "Removing all previous Salt keys..."
        /usr/bin/salt-key --delete-all --yes || /bin/true
    fi
else
    log "Running certs.sh in the Salt master"
    /srv/salt/certs/certs.sh

    log "Running the orchestration in the Salt master"
    salt-run state.orchestrate orch.kubernetes

    # dirs in the salt master
    CA_DIR=/srv/files
    OUT_DIR=/root

    # needed for the "host" utility
    zypper -n --no-gpg-checks in --force-resolution --no-recommends bind-utils

    if [ -n "$EXTRA_API_SRV_IP" ] ; then
        API_SERVER_IP=$EXTRA_API_SRV_IP
    else
        API_SERVER_IP=$(host $API_SERVER_DNS_NAME | grep "has address" | awk '{print $NF}')
        [ -n "$API_SERVER_IP" ] || abort "could not determine the IP of the API server with DNS"
    fi

    [ -f $CA_DIR/ca.crt ]   || abort "CA file does not exist"

    log "Generating certificates for 'kubectl' in the Salt master"
    [ -f $OUT_DIR/ca.crt ]    || cp $CA_DIR/ca.crt $OUT_DIR/ca.crt
    [ -f $OUT_DIR/admin.key ] || openssl genrsa -out $OUT_DIR/admin.key 2048
    [ -f $OUT_DIR/admin.csr ] || openssl req -new -key $OUT_DIR/admin.key \
        -out $OUT_DIR/admin.csr -subj "/CN=kube-admin"
    [ -f $OUT_DIR/admin.crt ] || openssl x509 -req \
        -in $OUT_DIR/admin.csr -CA $CA_DIR/ca.crt \
        -CAkey $CA_DIR/ca.key -CAcreateserial \
        -out $OUT_DIR/admin.crt -days 365

    log "Generating a 'kubeconfig' file"
    cat <<EOF > $OUT_DIR/kubeconfig
apiVersion: v1
clusters:
- cluster:
    certificate-authority: ca.crt
    server: https://${API_SERVER_IP}:${API_SERVER_PORT}/
  name: default-cluster
contexts:
- context:
{'fraction_non_alphanumeric': 0.15334807327858496, 'fraction_numerical': 0.011054958938723942, 'mean_word_length': 2.356120826709062, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5549618', 'n_tokens_mistral': 2421, 'n_tokens_neox': 2291, 'n_words': 588}