Exemplo n.º 1
0
        /// <summary>
        /// Register the runner with the coordinator
        /// </summary>
        private static void registerRunner()
        {
            // Read Token
            string sToken = "";

            while (sToken == "")
            {
                Console.WriteLine("Please enter the gitlab-ci token for this runner:");
                sToken = Console.ReadLine();
            }

            // Register Runner
            string sTok = Network.registerRunner(SSHKey.getPublicKey(), sToken);

            if (sTok != null)
            {
                // Save Config
                Config.token = sTok;
                Config.saveConfig();

                Console.WriteLine();
                Console.WriteLine("Runner registered successfully. Feel free to start it!");
            }
            else
            {
                Console.WriteLine();
                Console.WriteLine("Failed to register this runner. Perhaps your SSH key is invalid or you are having network problems");
            }
        }
            /// <summary>
            /// Deletes SSH key to user account. If _User is null adds for current user.
            /// </summary>
            /// <param name="_Config"></param>
            /// <param name="_SSHKey"></param>
            /// <param name="_User"></param>
            public static void Delete(Config _Config, SSHKey _SSHKey, User _User = null)
            {
                try
                {
                    string URI = _Config.APIUrl;

                    if (_User == null)
                    {
                        URI += "user/keys/" + _SSHKey.id.ToString();
                    }
                    else
                    {
                        URI += "users/" + _User.id.ToString() + "/keys/" + _SSHKey.id.ToString();
                    }

                    HttpResponse <string> R = Unirest.delete(URI)
                                              .header("accept", "application/json")
                                              .header("PRIVATE-TOKEN", _Config.APIKey)
                                              .asString();

                    if (R.Code < 200 || R.Code >= 300)
                    {
                        throw new GitLabServerErrorException(R.Body, R.Code);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            /// <summary>
            /// Adds SSH key to user account. If _User is null adds for current user.
            /// </summary>
            /// <param name="_Config"></param>
            /// <param name="_SSHKey"></param>
            /// <param name="_User"></param>
            public static void Add(Config _Config, SSHKey _SSHKey, User _User = null)
            {
                try
                {
                    string URI = _Config.APIUrl;

                    if (_User == null)
                    {
                        URI += "user/keys";
                    }
                    else
                    {
                        URI += "users/" + _User.id.ToString() + "/keys";
                    }

                    URI += "?title=" + HttpUtility.UrlEncode(_SSHKey.title)
                           + "&key=" + HttpUtility.UrlEncode(_SSHKey.key);

                    HttpResponse <string> R = Unirest.post(URI)
                                              .header("accept", "application/json")
                                              .header("PRIVATE-TOKEN", _Config.APIKey)
                                              .asString();

                    if (R.Code < 200 || R.Code >= 300)
                    {
                        throw new GitLabServerErrorException(R.Body, R.Code);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            /// <summary>
            /// Gets list of SSH Keys for a user. Gets keys for the current user if _User is null.
            /// </summary>
            /// <param name="_Config"></param>
            /// <param name="_User"></param>
            /// <returns></returns>
            public static List <SSHKey> List(Config _Config, User _User = null)
            {
                List <SSHKey> RetVal = new List <SSHKey>();

                try
                {
                    int           page = 1;
                    List <SSHKey> keys = new List <SSHKey>();

                    do
                    {
                        keys.Clear();

                        string URI = _Config.APIUrl;

                        if (_User == null)
                        {
                            URI += "user/keys";
                        }
                        else
                        {
                            URI += "users/" + _User.id.ToString() + "/keys";
                        }

                        URI += "?per_page=100" + "&page=" + page.ToString();

                        HttpResponse <string> R = Unirest.get(URI)
                                                  .header("accept", "application/json")
                                                  .header("PRIVATE-TOKEN", _Config.APIKey)
                                                  .asString();

                        if (R.Code < 200 || R.Code >= 300)
                        {
                            throw new GitLabServerErrorException(R.Body, R.Code);
                        }
                        else
                        {
                            dynamic Result = JsonConvert.DeserializeObject(R.Body);
                            if (Result is JArray)
                            {
                                JArray ResultArray = (JArray)Result;
                                foreach (JToken Token in ResultArray)
                                {
                                    SSHKey K = JsonConvert.DeserializeObject <SSHKey>(Token.ToString());
                                    keys.Add(K);
                                }
                            }
                        }
                        page++;
                        RetVal.AddRange(keys);
                    }while (keys.Count > 0 & page < 100);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return(RetVal);
            }
Exemplo n.º 5
0
        private static void Setup()
        {
            Config.GitLabEndpoint     = "http://192.168.1.112/";
            Config.GitLabPrivateToken = "r8pHJ3khRNsNY5yCZ9zK";

            Config.GitLabCIEndpoint = "http://192.168.1.112/gitlabci";
            Config.RegisterToken    = "4d2b9e8f8119d52e3e33";

            SSHKey.GenerateSSHKeyPair();
        }
Exemplo n.º 6
0
            void DeleteSSHKey(SSHKey _SSHKey)
            {
                if (Parent != null)
                {
                    if (this.id == Parent.CurrentUser.id)
                    {
                        SSHKey.Delete(Parent.CurrentConfig, _SSHKey);
                    }
                    else
                    {
                        SSHKey.Delete(Parent.CurrentConfig, _SSHKey, this);
                    }

                    this.RefreshSSHKeys();
                }
            }
Exemplo n.º 7
0
        /// <summary>
        /// Update an existing SSH Key. Note that this will only update newly installed machines. The key will not be updated on any existing machines.
        /// </summary>
        /// <param name="SSHKey">Unique identifier for this snapshot. These can be found Using the GetSSHKeys() Call.</param>
        /// <returns>No response, check HTTP result code.</returns>
        public SSHKeyUpdateResult UpdateSSHKey(SSHKey SSHKey)
        {
            var args = new List <KeyValuePair <string, object> >
            {
                new KeyValuePair <string, object>("name", SSHKey.name),
                new KeyValuePair <string, object>("SSHKEYID", SSHKey.SSHKEYID),
                new KeyValuePair <string, object>("ssh_key", SSHKey.ssh_key)
            };

            var response = ApiExecute <SSHKey>(
                "sshkey/update", ApiKey, args, ApiMethod.POST);

            return(new SSHKeyUpdateResult()
            {
                ApiResponse = response.Item1
            });
        }
Exemplo n.º 8
0
            void RefreshSSHKeys()
            {
                if (this.Parent != null)
                {
                    if (this.id == Parent.CurrentUser.id)
                    {
                        _SSHKeys = SSHKey.List(Parent.CurrentConfig);
                    }
                    else
                    {
                        _SSHKeys = SSHKey.List(Parent.CurrentConfig, this);
                    }

                    foreach (SSHKey k in _SSHKeys)
                    {
                        k.user = this;
                    }
                }
            }
Exemplo n.º 9
0
        /// <summary>
        /// Start the Setup
        /// </summary>
        public static void run()
        {
            Console.WriteLine("This seems to be the first run,");
            Console.WriteLine("please provide the following info to proceed:");
            Console.WriteLine();

            // Read coordinator URL
            String sCoordUrl = "";

            while (sCoordUrl == "")
            {
                Console.WriteLine("Please enter the gitlab-ci coordinator URL (e.g. http://gitlab-ci.org:3000/ )");
                sCoordUrl = Console.ReadLine();
            }
            Config.url = sCoordUrl;
            Console.WriteLine();

            // Generate SSH Keys
            SSHKey.generateKeypair();

            // Register Runner
            registerRunner();
        }