Beispiel #1
0
        protected override void ProcessRecord()
        {
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => Assembly.Load(AssemblyResolver.ResolveAssembly(args));
            ChefConfigurator chefConfigurator = new ChefConfigurator();

            chefConfigurator.LoadConfig();
            // ChefConfig.apiLog = false;

            // Default -Version to _latest
            if (Version == null)
            {
                Version = "_latest";
            }

            Cookbook   cookbook = new Cookbook();
            ReturnType rt       = cookbook.Download(Name, Version);

            if (rt.Result == 0)
            {
                Logger.log("ok", "Added cookbook: " + Name + "[" + Version + "] to cookbook path.");
            }
            else
            {
                Logger.log("error", rt.Message);
                Logger.log("error", "There is an error adding cookbook: " + Name + "[" + Version + "].");
                Terminate(rt.Message);
            }
        }
Beispiel #2
0
        public static void ConfigureApiClient()
        {
            ChefConfigurator chefConfigurator = new ChefConfigurator();

            chefConfigurator.LoadConfig();
            Logger.log("ok", "API Client is configured.");
        }
Beispiel #3
0
        protected override void ProcessRecord()
        {
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => Assembly.Load(AssemblyResolver.ResolveAssembly(args));
            ChefConfigurator chefConfigurator = new ChefConfigurator();

            chefConfigurator.LoadConfig();

            // Disable API log to console
            ChefConfig.ApiLog = false;

            // Start Sprint
            ExecuteSprint();
        }
Beispiel #4
0
        /// <summary>
        /// Main Processing function of the CmdLet
        /// </summary>
        protected override void ProcessRecord()
        {
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => Assembly.Load(AssemblyResolver.ResolveAssembly(args));
            ChefConfigurator chefConfigurator = new ChefConfigurator();

            chefConfigurator.LoadConfig();

            // 01. Testing BuildRunList method
            // BuildRunList();

            // 02. Testing Compilation
            TestCompile();
        }
Beispiel #5
0
        /// <summary>
        /// Loads configuration from HKLM\Software\Blu\Config
        /// Updates settings tab with this data
        /// </summary>
        private void LoadConfig()
        {
            ChefConfigurator chefConfigurator = new ChefConfigurator();

            chefConfigurator.LoadConfig();
            txtClientName.Text      = ChefConfig.ClientName;
            txtClientPath.Text      = ChefConfig.ClientPath;
            txtClientPem.Text       = ChefConfig.ClientPem;
            txtNodeName.Text        = ChefConfig.NodeName;
            txtOrganization.Text    = ChefConfig.Organization;
            txtOrganizationUri.Text = ChefConfig.OrganizationUri.ToString();
            txtValidationKey.Text   = ChefConfig.ValidationKey;
            txtValidator.Text       = ChefConfig.Validator;
        }
Beispiel #6
0
        /// <summary>
        /// Saves settings tab to ChefConfig
        /// Updates HKLM\Software\Blu\Config
        /// </summary>
        private void SaveConfig()
        {
            ChefConfigurator chefConfigurator = new ChefConfigurator();

            ChefConfig.ClientName      = txtClientName.Text;
            ChefConfig.ClientPath      = txtClientPath.Text;
            ChefConfig.ClientPem       = txtClientPem.Text;
            ChefConfig.NodeName        = txtNodeName.Text;
            ChefConfig.Organization    = txtOrganization.Text;
            ChefConfig.OrganizationUri = new Uri(txtOrganizationUri.Text);
            ChefConfig.ValidationKey   = txtValidationKey.Text;
            ChefConfig.Validator       = txtValidator.Text;
            chefConfigurator.SaveConfig();
            MessageBox.Show("Configuration is saved to HKLM\\Software\\Blu\\Config", "Save Config", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Beispiel #7
0
        /// <summary>
        /// Main Processing function of the CmdLet
        /// </summary>
        protected override void ProcessRecord()
        {
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => Assembly.Load(AssemblyResolver.ResolveAssembly(args));
            ChefConfigurator chefConfigurator = new ChefConfigurator();
            ReturnType       rt = chefConfigurator.LoadConfig();

            if (rt.Result == 0)
            {
                // Configuration is loaded from registry, try to connect
                Connect();
            }
            else
            {
                ProcessConfiguration();
            }
        }
Beispiel #8
0
        public void Connect()
        {
            // Check client connection
            Dictionary <string, dynamic> client = (Dictionary <string, dynamic>)ChefEndpoint.Get("clients/" + ChefConfig.ClientName, "/").Object;

            if (client.Count > 0)
            {
                // Show logo
                Host.UI.Write(ConsoleColor.DarkGray, Host.UI.RawUI.BackgroundColor, ChefConfig.BluLogo);
                Console.WriteLine();
                Logger.log("ok", "Connected!");
                Logger.log("info", "Client Name: " + ChefConfig.ClientName + " / Organization: " + ChefConfig.Organization);

                // Save Config
                ChefConfigurator chefConfigurator = new ChefConfigurator();
                chefConfigurator.SaveConfig();

                // Check if node exist
                Dictionary <string, dynamic> node = (Dictionary <string, dynamic>)ChefEndpoint.Get("nodes/" + ChefConfig.NodeName, "/").Object;
                if (node.Count > 0)
                {
                    Logger.log("ok", "Recieved Node json for: " + ChefConfig.NodeName);
                }
                else
                {
                    Console.WriteLine();
                    Logger.log("error", "Unable to retrieve Node data.");
                    Logger.log("info", "You are able to query Chef server by Use-ChefAPI CmdLet, but it seems the Chef node name you provided does not exist.");
                }
            }
            else
            {
                Console.WriteLine();
                string badNews = "Unable to connect as " + ChefConfig.ClientName + ".";
                Logger.log("error", badNews + " Please check if Chef client exists and the RSA key is valid.");
                Terminate(badNews);
            }
        }
Beispiel #9
0
        protected override void ProcessRecord()
        {
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => Assembly.Load(AssemblyResolver.ResolveAssembly(args));
            ChefConfigurator chefConfigurator = new ChefConfigurator();

            chefConfigurator.LoadConfig();

            // Disable API log to console
            // ChefConfig.apiLog = false;

            // Default values for Node, Path and Format parameters
            if (Select == null)
            {
                if (string.IsNullOrEmpty(ChefConfig.NodeName))
                {
                    Select = "nodes/" + ChefConfig.NodeName;
                }
                else
                {
                    Select = "nodes";
                }
            }
            else
            {
                // Trim the first / from -Endpoint
                Select = Select.TrimStart('/');
            }

            if (Format == null)
            {
                Format = "DICTIONARY";
            }
            if (Path == null)
            {
                Path = "/";
            }

            ReturnType rt = ChefEndpoint.Get(Select, Path);

            if (rt.Result == 0)
            {
                // Determine if data is encrypted
                string key = String.Empty;
                if (Secret != null)
                {
                    Format = "SECRET";
                    key    = Secret;
                }
                else if (SecretFile != null)
                {
                    Format = "SECRET";
                    key    = File.ReadAllText(ChefConfig.Root + "\\" + SecretFile);
                }

                switch (Format.ToUpper())
                {
                case "JSON":
                    WriteObject(rt.Data);
                    break;

                case "DICTIONARY":
                    WriteObject(rt.Object);
                    break;

                default:
                    Logger.log("error", "Output format is not recognized. Accepted values are 'Json', 'Dictionary'");
                    Terminate("Unrecognized Format");
                    break;
                }
            }
            else
            {
                Terminate(rt.Message);
            }
        }
Beispiel #10
0
        /// <summary>
        /// Bootstrap the machine providing the mandatory parameters
        /// </summary>
        public void Bootstrap()
        {
            // ══════════════════════════
            //  Check client.rb
            // ══════════════════════════

            ChefConfig.ClientRb = ChefConfig.Root + @"\client.rb";

            if (File.Exists(ChefConfig.ClientRb))
            {
                if (_force)
                {
                    Logger.log("warn", "client.rb file exists and -Force parameter is true. client.rb file will be overwritten.");
                    File.Delete(ChefConfig.ClientRb);
                }
                else
                {
                    const string badNews = "client.rb file exists. Do not Invoke-Bootstrap if chef-client is already configured.";
                    Logger.log("fatal", badNews);
                    Terminate(badNews);
                }
            }

            // ══════════════════════════
            //  Add Client
            // ══════════════════════════

            Client     client = new Client();
            ReturnType rt     = client.Add(ChefConfig.NodeName, _force);

            if (rt.Result == 0)
            {
                using (StreamWriter sw = new StreamWriter(ChefConfig.Root + @"\client.pem", false))
                {
                    sw.WriteLine(rt.Data);
                }
                Logger.log("ok", "Chef client is succesfully created. RSA key is saved as client.pem");
            }
            else
            {
                Logger.log("fatal", rt.Message);
                Terminate(rt.Message);
            }

            // ══════════════════════════
            //  Create client.rb
            // ══════════════════════════

            Dictionary <string, string> dict = new Dictionary <string, string>
            {
                { "chef_server_url", "\"" + ChefConfig.OrganizationUri + "\"" },
                { "node_name", "\"" + ChefConfig.NodeName + "\"" }
            };

            try
            {
                StreamWriter sw = new StreamWriter(ChefConfig.ClientRb);
                foreach (var param in dict)
                {
                    sw.WriteLine("{0} {1}", param.Key, param.Value);
                }
                sw.Close();
                Logger.log("ok", "Client.rb file is succesfully created.");
            }
            catch (Exception ex)
            {
                string badNews = "Error writing client.rb file: " + ex.Message;
                Logger.log("fatal", badNews);
                Terminate(badNews);
            }

            // ══════════════════════════
            //  Add Node
            // ══════════════════════════


            Node node = new Node();

            List <string> runlist = new List <string>();
            List <string> roles   = new List <string>();

            if (RunList != null)
            {
                runlist = RunList.Replace(" ", String.Empty).Split(',').ToList();
            }
            if (Roles != null)
            {
                roles = Roles.Replace(" ", String.Empty).Split(',').ToList();
            }

            rt = node.Add(ChefConfig.NodeName, Env, runlist, roles, _force);

            if (rt.Result == 0)
            {
                Logger.log("ok", "Chef Node is succesfully created.");
            }
            else
            {
                Logger.log("fatal", rt.Message);
                Terminate(rt.Message);
            }

            // ══════════════════════════
            //  Create and Save Config
            // ══════════════════════════

            // Adding ChefConfig parameters, needed for the next API connection using machine client account:
            ChefConfig.ClientName = ChefConfig.NodeName;
            ChefConfig.ClientPath = ChefConfig.Root;
            ChefConfig.ClientPem  = ChefConfig.Root + @"\client.pem";

            // Saving config
            ChefConfigurator chefConfigurator = new ChefConfigurator();

            rt = chefConfigurator.SaveConfig();
            Logger.log(rt);
            if (rt.Result == 4)
            {
                Terminate(rt.Message);
            }
        }