Monday, 13 May 2024

How do I undo the most recent local commits in Git?

I accidentally committed the wrong files to Git but didn't push the commit to the server yet.

How do I undo those commits from the local repository?

This is a simple Guide. Here are the steps to undo the last commit from your local repository for both Linux and Windows users:

Step 1: Open your terminal (Command Prompt for Windows users).

Step 2: Navigate to your git repository.
cd /path/to/your/repository
Step 3: Run the following command to undo the last commit:
git reset --soft HEAD~1

This command will undo the last commit and bring the changes back into your staging area (i.e., the changes will be “uncommitted” but still present).

Step 4: If you want to completely remove the changes, instead of just “uncommitting”, you can use the following command:
git reset --hard HEAD~1

This command will permanently remove the last commit. Be careful with this command, as it permanently discards committed changes.

Note: These commands work the same way in both Linux and Windows. Open the terminal (Linux) or Command Prompt (Windows) as your first step.

A More complex answer here 👇👇👇👇👇👇 


Undo a commit & redo

$ git commit -m "Something terribly misguided" # (0: Your Accident)
$ git reset HEAD~                              # (1)
[ edit files as necessary ]                    # (2)
$ git add .                                    # (3)
$ git commit -c ORIG_HEAD                      # (4)
  1. git reset is the command responsible for the undo. It will undo your last commit while leaving your working tree (the state of your files on disk) untouched. You'll need to add them again before you can commit them again.

git reset --hard <commit_id>

"Move" your HEAD back to the desired commit.


# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard HEAD~

# Alternatively, if there's work to keep (stash your changes before):
git stash -u
git reset --hard HEAD~
git stash pop
# This saves the modifications, then reapplies that patch after resetting.

# You could get merge conflicts if you've modified things which were
# changed since the commit you reset to.

enter image description here

  1. Make corrections to working tree files.
  2. git add anything that you want to include in your new commit.
  3. Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEADcommit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C option.

Alternatively, to edit the previous commit (or just its commit message)commit --amend will add changes within the current index to the previous commit.

To remove (not revert) a commit that has been pushed to the server, rewriting history with git push origin main --force[-with-lease] is necessary. It's almost always a bad idea to use --force; prefer --force-with-lease instead, and as noted in the git manual:

You should understand the implications of rewriting history if you amend a commit that has already been published.


Further Reading

You can use git reflog to determine the SHA-1 for the commit to which you wish to revert. Once you have this value, use the sequence of commands as explained above.


HEAD~ is the same as HEAD~1.

The article What is the HEAD in git? is helpful if you want to uncommit multiple commits.

Another answer which explain about HEAD and way to "recover" from commit mistakes:
How can I move HEAD back to a previous location? (Detached head) & Undo commits

Wednesday, 8 May 2024

DevOps for Small Businesses: Simplifying Deployment and Scaling

Introduction

Hey there, small business owners! Are you tired of software deployments that move at the speed of a sloth? Want to scale up faster than a caffeine-fueled coder on a deadline? It’s time to talk about DevOps, a superhero team-up of development and operations that's all about getting things done better and faster. Let’s dive into how you can zap those old-school methods and boost your business into the fast lane!



The Basics of DevOps for Small Businesses

What’s the Big Deal with DevOps?

DevOps isn't just for the big leagues; it's like the Swiss Army knife for your business—versatile, efficient, and surprisingly affordable. It blends development (the "Dev") with operations (the "Ops") into a dynamic duo. Think Batman and Robin, but for pushing updates and fixing bugs. Small businesses can particularly rock this by speeding up delivery times and nailing quality from the get-go.


Tools and Technologies

Picking Your Gadget Arsenal

Choosing the right tools is less about stocking up for an apocalypse and more about picking the niftiest gadgets for the job:
  • Version Control Systems (Git): This is your diary but for code. It remembers everything, so you don’t have to.
  • CI/CD Tools (Jenkins or CircleCI): These are your assembly lines, automating the nitty-gritty so you can sip coffee and brainstorm.
  • Monitoring Tools (Nagios, Prometheus): These are your lookouts, keeping an eagle eye on operations and squawking loudly if something goes haywire.
  • Configuration Management Tools (Ansible, Puppet): Think of these as your magical elves, setting up everything perfectly while you’re off handling other business spells.

Implementation Strategies

Your DevOps Game Plan

Jumping into DevOps might seem like leaping into a double-dutch jump rope game. Here’s how to not trip up:
  1. Look Before You Leap: Check out what you’re doing now and where a dash of DevOps could make life easier.
  2. Plan Like a Pro: Set some goals. Nothing fancy—just some milestones you’d like to hit with DevOps.
  3. Pick Your Tools Wisely: Gear up based on what you need, not what the cool kids are using.
  4. Start Small: Kick off a mini-project. It’s like your own pilot episode, so make it good!


Overcoming Challenges

Dodging the DevOps Pitfalls

Sure, roadblocks pop up, but here’s how to swerve around them:
  • Tiny Budgets: Embrace open-source tools. They’re free and often just as good as the pricey stuff.
  • Skill Shortages: Consider some quick training, or maybe even bring in a freelancer to help raise your team’s game.
  • Fear of Change: Change can be scary, but so was your first smartphone, right? And look at you now!

Case Studies

Success Stories to Make You Smile

Consider the local boutique that streamlined its online checkout system with DevOps magic, turning its once-glacial process into a sprinter’s paradise. They cut down issue resolution from days to hours. Now, that’s moving at the speed of business!

Conclusion and Next Steps

Take the DevOps Plunge

Think of DevOps as your business’s new best friend. It’s here to help you deploy faster, scale bigger, and react quicker to whatever the market throws at you. Start small, think big, and scale fast. Ready to become a DevOps believer? Dive into some resources, connect with experts, or just start tinkering. The future’s bright, and it’s streamlined!

References Used on This Blog:

- CircleCI - https://circleci.com/ 
- Nagios -  https://www.nagios.org/ 
- Prometheus - https://prometheus.io/ 

Thursday, 2 May 2024

How to Create a Ansible Lab on your Local Machine using Vagrant in 5 min using ChatGPT - Part 2

Update the above Vagrantfile Centos 8 servers and add Public IP and dhcp on each server ..

To update the provided Vagrantfile for the three CentOS 8 servers to each have a public IP and use DHCP for IP assignment, you'll need to modify the network configuration to use a public network. 

Vagrant can configure your VMs to use a public network, which allows them to access and be accessible from the public internet if your hosting environment supports it. This is often achieved by using DHCP to assign a public IP automatically.

Here's how you can modify the Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  # Define the base box
  config.vm.box = "centos/8"

  # Ansible Tower Server
  config.vm.define "ansible_tower" do |ansible_tower|
    ansible_tower.vm.hostname = "ansible-tower"
    ansible_tower.vm.network "public_network", use_dhcp_assigned_default_route: true
  end

  # Deployment Environment 1
  config.vm.define "deploy_env1" do |deploy_env1|
    deploy_env1.vm.hostname = "deploy-env1"
    deploy_env1.vm.network "public_network", use_dhcp_assigned_default_route: true
  end

  # Deployment Environment 2
  config.vm.define "deploy_env2" do |deploy_env2|
    deploy_env2.vm.hostname = "deploy-env2"
    deploy_env2.vm.network "public_network", use_dhcp_assigned_default_route: true
  end
end


This configuration uses public_network and sets use_dhcp_assigned_default_route: true to allow DHCP to assign public IPs to your VMs. Please note, the actual public IP assignment depends on your network setup and the capabilities of your host environment. In some cases, you might need to specify a bridge or make additional configurations to ensure your VMs are accessible from the public internet.


Remember, exposing your VMs to the public internet can have security implications. Ensure that you have appropriate security measures in place, such as firewalls and secure authentication methods.





🌐 Sources 

Thursday, 25 April 2024

How to Create a Ansible Lab on your Local Machine using Vagrant in 5 min using ChatGPT


This is an exciting experiment of mine as DevOps. As I am experimenting with the Tools available ... So, the quest is to "Vagrantfile which creates 3 instances of Centos 8 servers, each server shall be able to connect on the same network for easy communication"

Here is what we want to achieve:

- Build 3 servers to use Ansible

Jumped into via ChatGPT via Bing Copilot and created the following prompt.


"Hi Copilot, can you write a Vagrantfile which creates 3 instances of Centos 8 servers, each server shall be able to connect on the same network for easy communication, these instance serves are to be used as Ansible Lab environments comprised of one Ansible Tower and two deployments environments, please"


The output:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  # Define the base box
  config.vm.box = "centos/8"

  # Ansible Tower Server
  config.vm.define "ansible_tower" do |ansible_tower|
    ansible_tower.vm.hostname = "ansible-tower"
    ansible_tower.vm.network "private_network", ip: "192.168.56.10"
  end

  # Deployment Environment 1
  config.vm.define "deploy_env1" do |deploy_env1|
    deploy_env1.vm.hostname = "deploy-env1"
    deploy_env1.vm.network "private_network", ip: "192.168.56.11"
  end

  # Deployment Environment 2
  config.vm.define "deploy_env2" do |deploy_env2|
    deploy_env2.vm.hostname = "deploy-env2"
    deploy_env2.vm.network "private_network", ip: "192.168.56.12"
  end

  # Provisioning with Ansible (Optional)
  # config.vm.provision "ansible" do |ansible|
  #   ansible.playbook = "playbook.yml"
  # end
end

Source code on Git Hub repository.

To use this Vagrantfile:

  1. Install VirtualBox and Vagrant on your system.
  2. Save the above code into a file named Vagrantfile.
  3. Open a terminal and navigate to the directory containing the Vagrantfile.
  4. Run vagrant up to create the VMs.

This Vagrantfile configures each CentOS 8 server with a static IP on a private network, allowing them to communicate with each other. You can uncomment and adjust the Ansible provisioning block to run your Ansible playbooks automatically.

You’ll have three CentOS 8 servers (server1, server2, and server3) along with an Ansible Tower instance (ansible-tower) for your lab environment. Adjust memory and CPU settings as needed based on your system resources.

Happy Ansible-ing! 🚀

Next - 

To test the Ansible connection to the three CentOS 8 servers you've set up, you can use a simple Ansible playbook that employs the ping module. This module is specifically designed for connection testing and doesn't actually send ICMP pings but attempts an SSH connection and returns pong on success.

Here's an example playbook, test_connection.yml, that you can use:


yaml
--- - name: Test Ansible Connection to Servers hosts: all gather_facts: no tasks: - name: Test connectivity with ping module ansible.builtin.ping:

Before running the playbook, ensure you have an inventory file (inventory.ini or your preferred name) that lists the IPs or hostnames of your servers under a group [servers] or individually. Here's an example:

javascript
[servers] 192.168.56.10 ansible_user=your_username ansible_ssh_private_key_file=/path/to/private_key 192.168.56.11 ansible_user=your_username ansible_ssh_private_key_file=/path/to/private_key 192.168.56.12 ansible_user=your_username ansible_ssh_private_key_file=/path/to/private_key


Replace your_username and /path/to/private_key with the appropriate user and SSH key path for your setup.

To run the playbook and test the connection to all servers in your inventory, use the following command:

bash
ansible-playbook -i inventory.ini test_connection.yml

This command will attempt to connect to each server listed in inventory.ini and report back whether the connection was successful.
....

Nice !! Now, let us push this code further ..  I have tested the script on my machine and Made all 3 instances up and running.  Further improved the code and stored in Github source code here

Here the code on VisualCode:


Continue on  part 2


🌐 Sourcesmedium.com - How to Create Multiple Virtual Machines Using Vagrant Tool

stackoverflow.com - Two Operating Systems in a single Vagrantfile



How to check for open ports on Linux

Checking for open ports is among the first steps to secure your device. Listening services may be the entrance for attackers who may exploit...