<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Phoenix</title>
    <description>The latest articles on Forem by Phoenix (@phoenixdahdev).</description>
    <link>https://forem.com/phoenixdahdev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1650865%2Ff0324945-0a80-4222-8c84-e62190afa0cc.jpg</url>
      <title>Forem: Phoenix</title>
      <link>https://forem.com/phoenixdahdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/phoenixdahdev"/>
    <language>en</language>
    <item>
      <title>Automating User Management with a Bash Script</title>
      <dc:creator>Phoenix</dc:creator>
      <pubDate>Mon, 01 Jul 2024 15:59:19 +0000</pubDate>
      <link>https://forem.com/phoenixdahdev/automating-user-management-with-a-bash-script-347</link>
      <guid>https://forem.com/phoenixdahdev/automating-user-management-with-a-bash-script-347</guid>
      <description>&lt;p&gt;Managing users in a Linux environment can be a daunting task, especially when your organization grows rapidly. As a DevOps engineer, having an automated process to handle user creation, group assignments, and password generation can save a significant amount of time and reduce errors. In this article, I'll walk you through a bash script designed to streamline user management by reading a file containing usernames and groups, creating the necessary users and groups, setting up home directories, and logging all actions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Challenge&lt;/strong&gt;&lt;br&gt;
When new developers join your team, it's essential to ensure they have the necessary access and permissions. Manually creating each user, assigning them to the appropriate groups, and setting up their home directories can be error-prone and time-consuming. To solve this, we'll create a bash script that automates this process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Solution&lt;/strong&gt;&lt;br&gt;
Our script, create_users.sh, will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read a text file containing usernames and group names.&lt;/li&gt;
&lt;li&gt;Create users and groups as specified.&lt;/li&gt;
&lt;li&gt;Set up home directories with appropriate permissions.&lt;/li&gt;
&lt;li&gt;Generate random passwords for the users.&lt;/li&gt;
&lt;li&gt;Log all actions to /var/log/user_management.log.&lt;/li&gt;
&lt;li&gt;Store the generated passwords securely in /var/secure/user_passwords.csv.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Let's dive into the script.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Script&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Here's the complete create_users.sh script:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

# Logging and password storage files
LOG_FILE="/var/log/user_management.log"
PASSWORD_FILE="/var/secure/user_passwords.csv"

# Ensure the secure directory exists
mkdir -p /var/secure

# Ensure the log file exists and create it if it doesn't
touch $LOG_FILE

# Ensure the password file exists and set the correct permissions
touch $PASSWORD_FILE
chmod 600 $PASSWORD_FILE

# Combined log and echo function
log_and_echo() {
    message="$(date +'%Y-%m-%d %H:%M:%S') - $1"
    echo $message
    echo $message &amp;gt;&amp;gt; $LOG_FILE
}

# Function to generate random passwords
generate_password() {
    tr -dc 'A-Za-z0-9' &amp;lt;/dev/urandom | head -c 12
}

# Check if a file was provided
if [ -z "$1" ]; then
    echo "Usage: bash create_users.sh &amp;lt;name-of-text-file&amp;gt;"
    exit 1
fi

# Read the file line by line
while IFS=';' read -r username groups; do
    # Remove leading/trailing whitespaces
    username=$(echo $username | xargs)
    groups=$(echo $groups | xargs)

    # Skip empty lines
    if [ -z "$username" ]; then
        continue
    fi

    log_and_echo "Processing user: $username"

    # Check if user already exists
    if id -u "$username" &amp;gt;/dev/null 2&amp;gt;&amp;amp;1; then
        log_and_echo "User $username already exists. Skipping creation."
        continue
    fi

    # Create a user-specific group if it doesn't exist
    if ! getent group "$username" &amp;gt;/dev/null 2&amp;gt;&amp;amp;1; then
        groupadd "$username"
        log_and_echo "Group $username created."
    fi

    # Create the user with the specific group
    useradd -m -g "$username" "$username" &amp;amp;&amp;amp; log_and_echo "User $username created with primary group $username."

    # Assign additional groups
    IFS=',' read -ra additional_groups &amp;lt;&amp;lt;&amp;lt; "$groups"
    for group in "${additional_groups[@]}"; do
        group=$(echo $group | xargs)  # Remove leading/trailing whitespaces
        if [ -n "$group" ] &amp;amp;&amp;amp; ! getent group "$group" &amp;gt;/dev/null 2&amp;gt;&amp;amp;1; then
            groupadd "$group"
            log_and_echo "Group $group created."
        fi
        usermod -aG "$group" "$username" &amp;amp;&amp;amp; log_and_echo "User $username added to group $group."
    done

    # Generate a random password
    password=$(generate_password)

    # Set the user's password
    echo "$username:$password" | chpasswd &amp;amp;&amp;amp; log_and_echo "Password set for user $username."

    # Store the username and password securely
    echo "$username,$password" &amp;gt;&amp;gt; $PASSWORD_FILE

    # Set home directory permissions
    chmod 700 "/home/$username" &amp;amp;&amp;amp; log_and_echo "Permissions set for user $username's home directory."

done &amp;lt; "$1"

log_and_echo "User creation process completed."
echo "User creation process completed."


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How It Works&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Setup:&lt;/strong&gt; The script initializes logging and password storage files, ensuring the secure directory and files exist with appropriate permissions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Password Generation:&lt;/strong&gt; A function generates random 12-character passwords for new users.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Logging:&lt;/strong&gt; All actions are logged with timestamps for auditing purposes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Input Validation:&lt;/strong&gt; The script checks if a valid input file is provided and ensures the log file exists.&lt;/li&gt;
&lt;li&gt;Processing: The script reads the input file line by line, creating users and groups as specified, and handling any existing users or groups appropriately.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Home Directory Permissions:&lt;/strong&gt; The home directory for each user is created with secure permissions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Final Log:&lt;/strong&gt; A final message is logged once the user creation process is completed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Usage&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Prepare the Input File:
Create a text file named users.txt with the following content:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;light; sudo,dev,www-data
idimma; sudo
mayowa; dev,www-data
john; admin,dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Run the Script:
Make the script executable and run it with sudo to ensure it has the necessary permissions:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chmod +x create_users.sh
sudo ./create_users.sh users.txt

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Verifying the Results&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Check the Log File:&lt;br&gt;
&lt;code&gt;cat /var/log/user_management.log&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Check the Password File:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;sudo cat /var/secure/user_passwords.csv&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Verify Users and Groups:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;getent passwd light
getent group light
getent group sudo

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Automating user management in a Linux environment can significantly improve efficiency and reduce errors. By leveraging a bash script like create_users.sh, you can quickly and securely create users, assign groups, and set up home directories. This script ensures that all actions are logged and passwords are stored securely, making it a valuable tool for any DevOps engineer.&lt;/p&gt;

&lt;p&gt;To learn more about the HNG Internship and explore opportunities, visit:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://hng.tech/internship"&gt;HNG Internship&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://hng.tech/hire"&gt;HNG Hire&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://hng.tech/premium"&gt;HNG Premium&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>devops</category>
      <category>hng</category>
      <category>automation</category>
    </item>
    <item>
      <title>Solving Complex Backend Challenge: proper approach</title>
      <dc:creator>Phoenix</dc:creator>
      <pubDate>Thu, 27 Jun 2024 13:18:08 +0000</pubDate>
      <link>https://forem.com/phoenixdahdev/solving-complex-backend-challenge-proper-approach-2lc3</link>
      <guid>https://forem.com/phoenixdahdev/solving-complex-backend-challenge-proper-approach-2lc3</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
In the ever-evolving world of backend development, problem-solving is at the core of our work. Today, I want to share a recent challenging problem I faced and how I tackled it step-by-step. As I embark on the HNG Internship, I look forward to honing my skills and collaborating with talented developers. Here’s a glimpse into my backend journey.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Challenge: Optimizing Database Performance
&lt;/h3&gt;

&lt;p&gt;Recently, I encountered a significant performance issue with a web application I was working on. The application was experiencing slow response times, especially when handling complex queries. This was affecting the user experience and had to be resolved urgently.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 1: Identifying the Problem
&lt;/h4&gt;

&lt;p&gt;The first step was to identify the root cause of the slow performance. I used performance monitoring tools to analyze the application’s behavior and pinpointed that the database queries were taking an unusually long time to execute.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 2: Analyzing the Queries
&lt;/h4&gt;

&lt;p&gt;I extracted the slow queries and analyzed them. It became evident that the queries were not optimized and were fetching more data than necessary. Some queries were also performing full table scans, which further slowed down the performance.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 3: Optimizing the Queries
&lt;/h4&gt;

&lt;p&gt;To optimize the queries, I:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Indexed Key Columns&lt;/strong&gt;: I added indexes to the columns that were frequently used in the WHERE clause. This significantly reduced the query execution time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Query Refactoring&lt;/strong&gt;: I rewrote some of the complex queries to be more efficient. For instance, I replaced nested subqueries with JOIN operations where appropriate.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pagination&lt;/strong&gt;: For queries that fetched large datasets, I implemented pagination to limit the amount of data retrieved at a time.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Step 4: Database Configuration
&lt;/h4&gt;

&lt;p&gt;I also reviewed the database configuration settings and made several adjustments:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Increased Cache Size&lt;/strong&gt;: I increased the cache size to ensure frequently accessed data was kept in memory, reducing the need for disk I/O.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Connection Pooling&lt;/strong&gt;: Implemented connection pooling to manage database connections more efficiently.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Step 5: Monitoring and Testing
&lt;/h4&gt;

&lt;p&gt;After making these changes, I monitored the application to ensure the performance issues were resolved. I conducted stress testing to simulate high traffic and confirmed that the response times had improved significantly.&lt;/p&gt;

&lt;h3&gt;
  
  
  My Journey with HNG Internship
&lt;/h3&gt;

&lt;p&gt;Joining the HNG Internship is an exciting opportunity for me. I am looking forward to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enhancing my backend development skills.&lt;/li&gt;
&lt;li&gt;Collaborating with a diverse team of developers.&lt;/li&gt;
&lt;li&gt;Working on real-world projects that challenge me to grow.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The HNG Internship is known for its hands-on approach and supportive community, making it the perfect environment for me to thrive. If you’re interested in learning more about the HNG Internship, check out their &lt;a href="https://hng.tech/internship"&gt;internship page&lt;/a&gt; and &lt;a href="https://hng.tech/hire"&gt;hire page&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Solving complex backend problems is both challenging and rewarding. By systematically identifying and addressing performance bottlenecks, I was able to improve the application's responsiveness significantly. As I embark on my journey with the HNG Internship, I am excited to tackle more such challenges and grow as a backend developer.&lt;/p&gt;

&lt;p&gt;If you’re passionate about backend development and looking to elevate your skills, the HNG Internship is a fantastic opportunity to consider. Happy coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Exploring the Frontend Landscape: React vs. Svelte</title>
      <dc:creator>Phoenix</dc:creator>
      <pubDate>Thu, 27 Jun 2024 12:58:31 +0000</pubDate>
      <link>https://forem.com/phoenixdahdev/exploring-the-frontend-landscape-react-vs-svelte-44ng</link>
      <guid>https://forem.com/phoenixdahdev/exploring-the-frontend-landscape-react-vs-svelte-44ng</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fldaacpocha01l23bklzq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fldaacpocha01l23bklzq.png" alt="dom reaction" width="599" height="369"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
In the dynamic world of frontend development, choosing the right technology can be a game-changer. Today, we'll dive into a comparison between two popular frontend technologies: React and Svelte. We'll explore their differences, strengths, and why one might choose one over the other.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is React?&lt;/strong&gt;&lt;br&gt;
React, developed by Facebook, is a JavaScript library for building user interfaces. It's component-based, meaning UI is divided into reusable pieces. React has gained massive popularity due to its efficiency and the extensive ecosystem that surrounds it.&lt;/p&gt;

&lt;p&gt;Key Features of React:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Virtual DOM&lt;/strong&gt;: React uses a virtual DOM to optimize updates, making it fast and efficient.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Component-Based Architecture&lt;/strong&gt;: It promotes reusability and maintainability of code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Strong Community and Ecosystem&lt;/strong&gt;: A vast array of libraries, tools, and resources are available.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One-Way Data Binding&lt;/strong&gt;: This ensures better control over the data flow, leading to more predictable behavior.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;What is Svelte?&lt;/strong&gt;&lt;br&gt;
Svelte is a relatively newer player in the frontend world. Created by Rich Harris, Svelte shifts much of the work to compile time, producing highly efficient and minimal JavaScript code. Unlike React, Svelte does not use a virtual DOM.&lt;/p&gt;

&lt;h4&gt;
  
  
  Key Features of Svelte:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Compile-Time Optimization&lt;/strong&gt;: Svelte compiles your code to highly efficient JavaScript at build time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No Virtual DOM&lt;/strong&gt;: Direct manipulation of the DOM leads to faster performance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reactivity&lt;/strong&gt;: Svelte's reactivity is built into the language, making it simpler to manage state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Smaller Bundle Size&lt;/strong&gt;: Svelte applications generally have smaller bundle sizes, improving load times.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Comparing React and Svelte&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;: Svelte often outperforms React due to its compile-time optimization and lack of virtual DOM. For highly interactive applications, Svelte can be more efficient.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Ease of Learning&lt;/strong&gt;: React has a steeper learning curve, especially with its concepts like hooks and JSX. Svelte, on the other hand, feels more like plain JavaScript and HTML, making it easier for beginners to grasp.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Community and Ecosystem&lt;/strong&gt;: React has a mature and extensive ecosystem, with abundant resources and third-party libraries. Svelte's community is growing, but it's not as extensive as React's.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Development Experience&lt;/strong&gt;: React’s component-based architecture is robust and promotes reusability. Svelte’s approach, with less boilerplate code and built-in reactivity, often leads to a more pleasant development experience.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;My Journey with React in HNG Internship&lt;br&gt;
As a participant in the HNG Internship, I'm excited to dive deep into React. HNG emphasizes hands-on experience and real-world projects, which aligns perfectly with my desire to enhance my frontend skills. Through this program, I expect to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build complex, dynamic web applications.&lt;/li&gt;
&lt;li&gt;Collaborate with a diverse group of developers.&lt;/li&gt;
&lt;li&gt;Gain exposure to best practices in frontend development.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The HNG Internship is a fantastic opportunity to grow and learn in a supportive environment. If you're interested in knowing more about the HNG Internship, check out their &lt;a href="https://hng.tech/internship"&gt;internship page&lt;/a&gt; and &lt;a href="https://hng.tech/hire"&gt;hire page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Both React and Svelte offer unique advantages. React's mature ecosystem and component-based architecture make it a solid choice for many projects. Svelte's innovative approach to reactivity and performance optimization presents an exciting alternative. Choosing between them depends on your project requirements and personal preference.&lt;/p&gt;

&lt;p&gt;If you're a developer looking to expand your frontend skills, the HNG Internship is a great platform to explore and grow. Happy coding!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
